标签:des style blog http color io strong for 数据
Description
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
Input
Output
Sample Input
0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0
Sample Output
(0, 0) (1, 0) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4)
1 #include<cstdio> 2 #include<string.h> 3 using namespace std; 4 int maze[5][5]; 5 int q[10000]; 6 int dx[4]={-1,1,0,0}; 7 int dy[4]={0,0,-1,1}; 8 int vis[5][5]; 9 int fa[5][5],dist[5][5],dir[5][5]; 10 int dirt[10000]; 11 int zuobiao[10000]; 12 void bfs(int x,int y)//(x,y)是起点 13 { 14 int front=0,rear=0,d,u; 15 u=5*x+y; 16 vis[x][y]=1;fa[x][y]=u;dist[x][y]=0; 17 q[rear++]=u; 18 while(front<rear) 19 { 20 u=q[front++]; 21 x=u/5;y=u%5; 22 if(x==4&&y==4) break; 23 for(d=0;d<4;d++) 24 { 25 int nx=x+dx[d],ny=y+dy[d]; 26 if(nx>=0&&nx<5&&ny>=0&&ny<5&&!maze[nx][ny]&&!vis[nx][ny]) 27 { 28 int v=nx*5+ny; 29 q[rear++]=v; 30 vis[nx][ny]=1; 31 fa[nx][ny]=u; 32 dist[nx][ny]=dist[x][y]+1; 33 dir[nx][ny]=d; 34 } 35 } 36 } 37 } 38 39 void print_path(int x,int y) 40 { 41 int c=0; 42 for(;;) 43 { 44 int fx=fa[x][y]/5; 45 int fy=fa[x][y]%5; 46 if(fx==x&&fy==y) break; 47 dirt[c++]=dir[x][y]; 48 zuobiao[c-1]=fa[x][y]; 49 x=fx; 50 y=fy; 51 } 52 printf("(0, 0)\n"); 53 while(c--) 54 printf("(%d, %d)\n",zuobiao[c]/5+dx[dirt[c]],zuobiao[c]%5+dy[dirt[c]]); 55 } 56 57 int main() 58 { 59 int i,j; 60 for(i=0;i<5;i++) 61 for(j=0;j<5;j++) 62 { 63 scanf("%d",&maze[i][j]); 64 } 65 bfs(0,0); 66 print_path(4,4); 67 return 0; 68 }
标签:des style blog http color io strong for 数据
原文地址:http://www.cnblogs.com/angledamon/p/3930240.html