Atcoder - abc453_d Go Straight

AT_abc453_d [ABC453D] Go Straight

写在前面

因为 \(Atcoder\) 没有提供 \(markdown\) 源码,所以我用的是洛谷的题面,没有中文。

为给大家带来的不便深表歉意。

(其实这些字只是为了凑齐中文字数,这样才能交上去)

题目描述

There is a grid of H rows \\times W columns, and Takahashi moves through this grid up, down, left, and right.

The state of the cell at the i -th row from the top and j -th column from the left (1\\leq i\\leq H, 1\\leq j\\leq W) is represented by the character S_{i,j} .

S_{i,j} is one of #, ., o, x, S, G.

  • If S_{i,j}= #: This cell cannot be entered.
  • If S_{i,j}= .: This cell can be freely entered and exited. That is, after entering this cell, Takahashi can move to any adjacent cell (if it exists) in the up, down, left, or right direction.
  • If S_{i,j}= o: In this cell, Takahashi must move in the same direction as the immediately preceding move. That is, after entering this cell, he must move to the next cell without changing direction.
  • If S_{i,j}= x: In this cell, Takahashi cannot move in the same direction as the immediately preceding move. That is, after entering this cell, he must change direction to move to the next cell. Turning 180 degrees to return to the previous cell is considered as changing direction.
  • If S_{i,j}= S: This cell is Takahashi's starting position. This cell can be freely entered and exited.
  • If S_{i,j}= G: This cell is Takahashi's destination. This cell can be freely entered and exited.

There is exactly one (i,j) with 1\\leq i\\leq H, 1\\leq j\\leq W satisfying S_{i,j}= S, and exactly one satisfying S_{i,j}= G.

Takahashi wants to reach the destination by repeatedly moving to adjacent cells up, down, left, or right from his starting position.

Determine whether this is possible, and if so, output one valid sequence of moves with at most 5\\times 10\^6 moves between adjacent cells.

It can be proved that if a valid sequence of moves exists under the problem's conditions, then there exists one with at most 5\\times 10\^6 moves.

As long as the number of moves is at most 5\\times 10\^6 , it is not necessary to minimize the number of moves.

输入格式

The input is given from Standard Input in the following format:

H W S_{1,1} S_{1,2} \\ldots S_{1,W} S_{2,1} S_{2,2} \\ldots S_{2,W} \\vdots S_{H,1} S_{H,2} \\ldots S_{H,W}

输出格式

If it is impossible to reach the destination while satisfying the conditions, output No on the first line, and nothing on the second line.

If it is possible to reach the destination while satisfying the conditions, output Yes on the first line.

On the second line, output a string T representing a sequence of moves. T must satisfy all of the following conditions.

  • The length \\lvert T\\rvert of T is between 1 and 5\\times 10\^6 , inclusive.
  • Each character of T is one of U, D, L, R.
    • The i -th character of T being U, D, L, R means that in the i -th move after departing, Takahashi moves to the adjacent cell above, below, to the left, or to the right, respectively.
  • For 1\\leq i\\leq \\lvert T\\rvert , Takahashi is not outside the grid after the i -th move.
  • During the moves, the conditions of each cell are not violated.
  • After the \\lvert T\\rvert -th move, Takahashi is at the destination cell. As long as this condition is satisfied, he may pass through the destination cell before the \\lvert T\\rvert -th move.

输入输出样例 #1

输入 #1

复制代码
3 5
.#...
.Sooo
..x.G

输出 #1

复制代码
Yes
DRUUDDRR

输入输出样例 #2

输入 #2

复制代码
3 3
#So
xoG
..#

输出 #2

复制代码
Yes
DDLURR

输入输出样例 #3

输入 #3

复制代码
2 2
So
oG

输出 #3

复制代码
No

说明/提示

Sample Explanation 1

Let cell (i,j) denote the cell at the i -th row from the top and j -th column from the left.

Following the sample output, the path goes: cell (2,2) \\to cell (3,2) \\to cell (3,3) \\to cell (2,3) \\to cell (1,3) \\to cell (2,3) \\to cell (3,3) \\to cell (3,4) \\to cell (3,5) , reaching the destination.

Other solutions such as DRUURLRRDD are also accepted. On the other hand, since Takahashi cannot go straight through cell (3,3) , solutions such as DRRR are not accepted.

Sample Explanation 3

It is impossible to reach the destination while satisfying the conditions.

Constraints

  • 1 \\leq H,W\\leq 1000
  • S_{i,j} is one of #, ., o, x, S, G.
  • There is exactly one (i,j) satisfying S_{i,j}= S and exactly one satisfying S_{i,j}= G.
  • H and W are integers.

思路概述

一眼 \(搜索\) 。 我用的是 \(DFS\) ;

我们用一个字符串来记录走过的路径,在搜索时附加上方向。

如果当前只能继续往前走 , 那么我们就只搜索向前。

如果我们不能再往前走,那么我们设置一个变量 ban ,表示不能走的方向。(特别地,如果当前在空地,ban是\(-1\))。

理论正确,开⌨!

代码

cpp 复制代码
#include <bits/stdc++.h>
#define maxn 1010
using namespace std;
const char ch[]={' ','R','D','L','U'};
const int dx[]={0,0,1,0,-1};
const int dy[]={0,1,0,-1,0};
int h,w,sx,sy,nx,ny;
char s[maxn][maxn];
bool vis[maxn][maxn][6];
string road;
bool F_end;
string op;
void dfs(int i,int j,short flag){
    if(i<1 || j<1 || i>h || j>w) return;
    if(vis[i][j][flag]) return;
    vis[i][j][flag]=true;
    if(F_end) return;
    if(s[i][j]=='#') return;
    if (s[i][j]=='G') {
        road=op;
        F_end=1;
        return;
    }
    if(s[i][j]=='o'){
        nx=i+dx[flag];
        ny=j+dy[flag];
        op+=ch[flag];
        dfs(nx,ny,flag);
        op.pop_back();
        return ;
    }
    short ban=-1;
    if(s[i][j]=='x') ban=flag;
    for(int k=1;k<=4;k++) {
        if(k==ban) continue;
        nx=i+dx[k];
        ny=j+dy[k];
        op+=ch[k];
        dfs(nx,ny,k);
        op.pop_back();
    }
}
int main() {
    cin>>h>>w;
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            cin>>s[i][j];
            if(s[i][j]=='S') sx=i,sy=j;
        }
    }
    dfs(sx,sy,-1);
    if(!F_end){
        cout<<"No";
        return 0;
    }
    cout<<"Yes\n";
    cout<<road;
    return 0;
}
相关推荐
承渊政道3 天前
【递归、搜索与回溯算法】(递归问题拆解与经典模型实战大秘笈)
数据结构·c++·学习·算法·macos·dfs·bfs
无小道9 天前
Mysql——索引
mysql·索引·搜索
色空大师13 天前
【ElasticSearch(ES)】安装及介绍
大数据·elasticsearch·搜索引擎·搜索
独断万古他化20 天前
【算法通关】二叉树中的深搜:DFS 递归解题套路
算法·二叉树·深度优先·dfs·递归
落地加湿器1 个月前
Acwing算法课图论与搜索笔记
c++·笔记·算法·图论·dfs·bfs·图搜索算法
无尽的罚坐人生1 个月前
hot 100 200. 岛屿数量
算法·dfs
像污秽一样1 个月前
算法设计与分析-习题9.2
数据结构·算法·排序算法·dfs
像污秽一样1 个月前
算法设计与分析-习题9.1
数据结构·算法·dfs·dp·贪婪
像污秽一样1 个月前
算法设计与分析-习题8.2
数据结构·算法·排序算法·dfs·化简