1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
| #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
using namespace std;
#define N 30
#define M 50
char map[N][M];
int dir[4][2]={{1,0},{0,-1},{0,1},{-1,0}};//D<L<R<U
char ch[4]={'D','L','R','U'};
int vis[N][M]={0};
struct point
{
int x,y;
string road;
point(int a,int b)
{
x=a;
y=b;
}
};
void bfs()
{
queue<point> q;
point p(0,0);
p.road="";
q.push(p);
vis[0][0]=1;
while(!q.empty())
{
point t=q.front();
q.pop();
if(t.x==N-1&&t.y==M-1)
{
cout<<t.road<<endl;
break;
}
for(int i=0;i<4;i++)
{
int dx=t.x+dir[i][0];
int dy=t.y+dir[i][1];
if(dx>=0&&dx<N&&dy>=0&&dy<M)
{
if(map[dx][dy]=='0'&&!vis[dx][dy])
{
point tt(dx,dy);
tt.road=t.road+ch[i];//记录路径
q.push(tt);
vis[dx][dy]=1;
}
}
}
}
}
int main()
{
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
scanf("%c",&map[i][j]);
getchar();//读掉回车
}
bfs();
return 0;
}
|