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;
}
相关推荐
多喝开水少熬夜2 天前
dfs思路回溯
算法·深度优先·dfs
辰域2 天前
从0到1帮佛山工厂拿到询盘:我们是怎么用AI搜索优化+短视频打通获客链路的?
企业·搜索·获客·工厂·流量
hnjzsyjyj4 天前
洛谷B3862:图的遍历(简单版)← 邻接表
dfs·邻接表
星马梦缘7 天前
算法设计与分析 作业二 答案与解析
算法·图论·dfs·bfs·floyd-warshall·bellman_ford·多源最短路
westdata-Tm7 天前
洛谷P1219 [USACO1.5] 八皇后 Checker Challenge
算法·深度优先·dfs
hnjzsyjyj9 天前
洛谷 B3622:枚举子集(递归实现指数型枚举)← DFS
数据结构·dfs
hnjzsyjyj11 天前
全排列问题DFS实现执行示意图
数据结构·dfs
XLYcmy11 天前
2026游戏安全技术竞赛-PC客户端安全-初赛 求解起点到终点的最短路径
windows·python·网络安全·dfs·bfs·游戏安全·曼哈顿距离
牢姐与蒯13 天前
c++数据结构之二叉搜索树
数据结构·c++·搜索
Tisfy16 天前
LeetCode 1722.执行交换操作后的最小汉明距离:连通图
算法·leetcode·dfs·题解·深度优先搜索·连通图