http://oj.ecustacm.cn/problem.php?id=1455
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
const int N=55;
int n,m;
string g[N];
int dist[N][N];
int dx[4]={1,0,0,-1},dy[4]={0,-1,1,0};
char dir[4]={'D','L','R','U'};
void bfs()//先找到路径
{
queue<pair<int,int>> q;
memset(dist,-1,sizeof dist);
dist[n-1][m-1]=0;
q.push({n-1,m-1});
while(q.size())
{
auto t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int x=t.first+dx[i], y=t.second+dy[i];
if(x>=0&&x<n&&&y>=0&&y<m&&dist[x][y]==-1&&g[x][y]=='0')
{
dist[x][y]=dist[t.first][t.second]+1;
q.push({x,y});
}
}
}
}
int main(void)
{
cin>>n>>m;
for(int i=0;i<n;i++) cin>>g[i];
bfs();
int x=0,y=0;
string res;
while(x!=n-1||y!=m-1)
{
for(int i=0;i<4;i++)
{
int nx=x+dx[i],ny=y+dy[i];
if(nx>=0&&nx<n&&ny>=0&&ny<m&&g[nx][ny]=='0')
{
if(dist[x][y]==1+dist[nx][ny] )
{
x=nx,y=ny;
res+=dir[i];
break;
}
}
}
}
cout<<res<<endl;
return 0;
}
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
using namespace std;
int n;
char a[105][105];
bool vis[105][105];
int dx[4]={1,0,0,-1};
int dy[4]={0,-1,1,0};
char str[4]={'D','L','R','U'};
struct node
{
int x,y;
string s;
}Node;
void bfs(int x,int y)
{
Node.x=0,Node.y=0,Node.s="";
queue<node>q; q.push(Node);
while(!q.empty())
{
node top=q.front(); q.pop();
for(int i=0;i<4;i++)
{
int tempx=top.x+dx[i];
int tempy=top.y+dy[i];
char c=str[i];
if(tempx>=0&&tempx<n&&tempy>=0&&tempy<50&&a[tempx][tempy]=='0'&&!vis[tempx][tempy])
{
node m;
m.x=tempx,m.y=tempy,m.s=top.s+c;
vis[tempx][tempy]=true;
if(m.x==n-1&&m.y==50-1)
{
cout<<m.s<<endl;
return;
}
q.push(m);
}
}
}
}
int main(void)
{
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
vis[0][0]=true;
bfs(0,0);
//cout<<"DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR"<<endl;
return 0;
}