Description
Super scholar robs the all homework of others. The monitor decides to combat with the super scholar so as to help students to get back the homework. But super scholar lives in a castle because he doesn’t want to be disturbed. The outside of the castle is a maze with two dimension grids. Entering the castle must pass the maze. The monitor needs to save time because of accompanying his girlfriend, so he wants to make a student named Huachao Wei help him who hasn’t a girlfriend. Now he has the map of the maze and needs to calculate the shortest path.
Input
The input consists several cases.For each case starts with two integers n,m(),symbolizing the length and width of the maze.The next n lines contain m numbers with no space and value for only 0 and 1.The essence is that 0 can pass ,1 can’t pass. Now you are in the place of (1,1) refering to the top left corner.the export is in the place of (n,m).Each step can only walk with up,down,left and right.
Output
For each case,the first line prints the least number of steps to arrive the castle.The second line prints k characters for U,D,L,R,representing up,down,left,right.if there are many paths with the same length,please print the path with Minimum dictionary.It is guarantees that there must have a path for arriving the castle.
Sample Input
3 3
001
100
110
3 3
000
000
000
Sample Output
4
RDRD
4
DDRR
Solution
bfs求最短路并记录路径,没啥好说的- -除了方向问题
可能真的是lucky girl了,比赛前一天一直在莽bfs
今晚真的不行了- -写不动题了
关于方向:注意xy和坐标轴的xy不一样
代码
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
char b[11][11];
int a[11][11],m,n;
typedef struct node
{
int x,y;
}s;
struct qq
{
int qx,qy;
char dir;
int l=0;
}q[11][11];
void bfs(int x,int y)
{
int dx[4]={0,-1,1,0},dy[4]={-1,0,0,1};
queue<node>h;
node t;
t.x=x;
t.y=y;
a[x][y]=1;
h.push(t);
while(!h.empty())
{
node s,u;
s=h.front();
h.pop();
for(int i=0;i<4;++i)
{
u.x=s.x+dx[i];
u.y=s.y+dy[i];
if(u.x>=0&&u.x<n&&u.y>=0&&u.y<m&&a[u.x][u.y]==0) //
{
h.push(u);
q[u.x][u.y].qx=s.x;
q[u.x][u.y].qy=s.y;
q[u.x][u.y].l=q[s.x][s.y].l+1;
if(i==0) q[u.x][u.y].dir='L';
if(i==1) q[u.x][u.y].dir='U';
if(i==2) q[u.x][u.y].dir='D';
if(i==3) q[u.x][u.y].dir='R';
a[u.x][u.y]=1;
}
if(x==n-1&&y==m-1) return;
}
}
}
void print(int x,int y)
{
if(x==0&&y==0)
{
return;
}
int ab=q[x][y].qx;
int bc=q[x][y].qy;
print(ab,bc);
printf("%c",q[x][y].dir);
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=0;i<n;++i) scanf("%s",b[i]);
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
//printf("%c",b[i][j]);
if(b[i][j]=='0') a[i][j]=0;
else a[i][j]=1;
}
//printf("\n");
}
bfs(0,0);
printf("%d\n",q[n-1][m-1].l);
print(n-1,m-1);
printf("\n");
}
return 0;
}