Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=1240

bfs

我的代码
 1 #include <cstdio>
2 #include <cstring>
3 #include <queue>
4 using namespace std;
5 const int N=15;
6 const int dx[6]={1,0,0,-1,0,0};
7 const int dy[6]={0,1,0,0,-1,0};
8 const int dz[6]={0,0,1,0,0,-1};
9 struct coor
10 {
11 int x,y,z;
12 };
13 queue<coor> q;
14 char maze[N][N][N];
15 int vis[N][N][N],dis[N][N][N];
16 int n,sx,sy,sz,ex,ey,ez;
17 int bfs(int x,int y,int z)
18 {
19 if (x==ex && y==ey && z==ez) return 0;
20 while (!q.empty()) q.pop();
21 vis[x][y][z]=1; dis[x][y][z]=0;
22 coor u={x,y,z};
23 q.push(u);
24 int nx,ny,nz,d;
25 while (!q.empty())
26 {
27 u=q.front(); q.pop();
28 x=u.x; y=u.y; z=u.z;
29 for (d=0;d<6;d++)
30 {
31 nx=x+dx[d]; ny=y+dy[d]; nz=z+dz[d];
32 if (nx<0 || nx>=n || ny<0 || ny>=n || nz<0 || nz>=n) continue;
33 if (vis[nx][ny][nz] || maze[nx][ny][nz]=='X') continue;
34 vis[nx][ny][nz]=1;
35 dis[nx][ny][nz]=dis[x][y][z]+1;
36 if (nx==ex && ny==ey && nz==ez) return dis[nx][ny][nz];
37 coor v={nx,ny,nz};
38 q.push(v);
39 }
40 }
41 return -1;
42 }
43 int main()
44 {
45 char temp[10];
46 int x,y,z,ans;
47 while (~scanf("%s%d",temp,&n))
48 {
49 getchar();
50 for (z=0;z<n;z++)
51 for (x=0;x<n;x++)
52 {
53 for (y=0;y<n;y++) maze[x][y][z]=getchar();
54 getchar();
55 }
56 scanf("%d%d%d%d%d%d",&sx,&sy,&sz,&ex,&ey,&ez);
57 scanf("%s",temp);
58 ans=bfs(sx,sy,sz);
59 if (ans<0) printf("NO ROUTE\n");
60 else printf("%d %d\n",n,ans);
61 }
62 return 0;
63 }

 

posted on 2012-01-15 20:31  Qiuqiqiu  阅读(181)  评论(0编辑  收藏  举报