标签:des style blog class code java tar javascript ext strong color
Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 3159 Accepted
Submission(s): 2106
这道题是三维BFS搜索,类似于 hdu 1253:胜利大逃亡(基础广搜BFS) 。
for(i=0;i<n;i++) //输入地图
for(j=0;j<n;j++)
for(k=0;k<n;k++)
cin>>a[j][k][i];
代码:
1 #include <iostream>
2 #include <string.h>
3 #include <queue>
4 using namespace std;
5 char a[11][11][11]; //记录地图
6 bool isv[11][11][11]; //记录访问过没有
7 int dx[6] = {0,1,0,-1,0,0};
8 int dy[6] = {1,0,-1,0,0,0};
9 int dz[6] = {0,0,0,0,-1,1};
10 int n;
11 int sx,sy,sz,ex,ey,ez;
12 struct NODE{
13 int x;
14 int y;
15 int z;
16 int step;
17 };
18 bool judge(int x,int y,int z)
19 {
20 if( x<0 || y<0 || z<0 || x>=n || y>=n || z>=n ) //出界
21 return 1;
22 if( isv[x][y][z] ) //走过
23 return 1;
24 if( a[x][y][z]==‘X‘ ) //遇到墙
25 return 1;
26 return 0;
27 }
28 int bfs() //返回到达终点的时间
29 {
30 memset(isv,0,sizeof(isv));
31 queue <NODE> q;
32 NODE cur,next;
33 cur.x = sx;
34 cur.y = sy;
35 cur.z = sz;
36 cur.step = 0;
37 isv[sx][sy][sz] = true;
38 q.push(cur); //第一个节点入队
39 while(!q.empty()){
40 cur = q.front();
41 q.pop(); //队首出队
42 if( cur.x==ex && cur.y==ey && cur.z==ez){
43 return cur.step;
44 }
45 for(int i=0;i<6;i++){
46 int nx = cur.x + dx[i];
47 int ny = cur.y + dy[i];
48 int nz = cur.z + dz[i];
49 if(judge(nx,ny,nz)) //判定
50 continue;
51 //可以走
52 next.x = nx;
53 next.y = ny;
54 next.z = nz;
55 isv[nx][ny][nz] = true; //记录访问过
56 next.step = cur.step + 1;
57 q.push(next);
58 }
59 }
60 return -1;
61 }
62 int main()
63 {
64 char str[20];
65 int i,j,k;
66 while(cin>>str>>n){
67 for(i=0;i<n;i++) //输入地图
68 for(j=0;j<n;j++)
69 for(k=0;k<n;k++)
70 cin>>a[j][k][i];
71 cin>>sx>>sy>>sz;
72 cin>>ex>>ey>>ez;
73 cin>>str;
74 int step = bfs();
75 if(step!=-1) //到达终点
76 cout<<n<<‘ ‘<<step<<endl;
77 else
78 cout<<"NO ROUTE"<<endl;
79 }
80 return 0;
81 }
Freecode : www.cnblogs.com/yym2013
hdu 1240:Asteroids!(三维BFS搜索),码迷,mamicode.com
标签:des style blog class code java tar javascript ext strong color
原文地址:http://www.cnblogs.com/yym2013/p/3701445.html