HDU 1253 胜利大逃亡
这一题是又裸又水的一道BFS题,唯一复杂的地方就是扩展到了三维,但是跟二维的区别不大。直接看代码吧:
AC code:
 View Code
View Code 
1 #include <iostream>
2 #define MAX 50
3 #define SZ 51
4 using namespace std;
5 struct node{
6 int x, y, z;
7 int step;
8 }que[MAX*MAX*MAX*2];
9 int a, b, c, t, flag;
10 int map[SZ][SZ][SZ];
11 int d[][3]={-1,0,0,1,0,0,0,-1,0,0,1,0,0,0,-1,0,0,1};//用于控制每次搜索的方向
12
13 bool inmap(int x, int y, int z)//用来判断新搜到的点是否在图的范围内
14 {
15 return x >= 0 && x < a && y >= 0 && y < b && z >= 0 && z < c;
16 }
17 int bfs()
18 {
19 flag = 0;
20 int front = 0, rear = 1;
21 que[0].x = 0; que[0].y = 0; que[0].z = 0; que[0].step = 0;
22 map[0][0][0] = 1;//向队列里插入第一个结点
23 while(front < rear)
24 {
25 if(que[front].x == a - 1 && que[front].y == b - 1 && que[front].z == c - 1 && que[front].step <= t)//在指定时间内找到重点
26 {
27 return que[front].step;
28 }
29 if(que[front].step > t)//超时
30 {
31 return - 1;
32 }
33 for(int i = 0; i < 6; i++)//因为是三维,所以要搜六个方向
34 {
35 int tx = que[front].x + d[i][0];
36 int ty = que[front].y + d[i][1];
37 int tz = que[front].z + d[i][2];
38 if(inmap(tx, ty, tz) && !map[tx][ty][tz])//所搜到的点可行,将新点加入到队尾
39 {
40 map[tx][ty][tz] = 1;
41 que[rear].x = tx;
42 que[rear].y = ty;
43 que[rear].z = tz;
44 que[rear++].step = que[front].step + 1;
45 }
46 }
47 front++;
48 }
49 return -1;
50 }
51
52
53 int main()
54 {
55 int cas;
56 scanf("%d", &cas);
57 while(cas--)
58 {
59 scanf("%d%d%d%d", &a, &b, &c, &t);
60 int i, j, k;
61 memset(map, 0, sizeof(map));
62 for(i = 0; i < a; i++)
63 {
64 for(j = 0; j < b; j++)
65 {
66 for(k = 0; k < c; k++)
67 {
68 scanf("%d", &map[i][j][k]);
69 }
70 }
71 }
72 printf("%d\n", bfs());
73 }
74 return 0;
75 }
 
                    
                 

