#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e3+5;
ll mp[maxn][maxn],vis[maxn][maxn],way[maxn][maxn];
ll delta[4][2]={{1,0},{0,-1},{0,1},{-1,0}};
char ans[maxn];
char cc[5]="DLRU";
ll n,m;
struct node{
ll x,y;
};
int main()
{
cin>>n>>m;
for(ll i=1;i<=n;i++){
for(ll j=1;j<=m;j++){
char x;cin>>x;
mp[i][j]=x-'0';
}
}
queue<node> q;
node tmp={1,1};
q.push(tmp);vis[1][1]=1;
while(!q.empty()){
node now=q.front();
q.pop();
for(ll k=0;k<4;k++){
ll x=now.x+delta[k][0],y=now.y+delta[k][1];
if(x<1||y<1||x>n||y>m||vis[x][y]||mp[x][y]) continue;
vis[x][y]=1;way[x][y]=k;
node tmp={x,y};
q.push(tmp);
}
}
ll x=n;ll y=m;ll num=0;
while(x!=1||y!=1){
ll k=way[x][y];
ans[num++]=cc[k];
x-=delta[k][0],y-=delta[k][1];
}
for(ll i=strlen(ans)-1;i>=0;i--){
cout<<ans[i];
}
}
/*
4 6
010000
000100
001001
110000
*/
【图论】BFS走迷宫问题
猜你喜欢
转载自blog.csdn.net/Rainfoo/article/details/104534749
今日推荐
周排行