hdu 1253 3维迷宫 在规定时间内能否出迷宫 (3维BFS)

题意:有一个人要在魔王回来之前逃出城堡。1表示墙,0表示路。魔王将在T分钟后回到城堡 起点可以是墙,但是人能走出。而终点也可以是墙,那自然就走不出了,但是要判断。

剪枝:如果终点是门或者从起点到终点的最短时间都大于t ,直接输出 -1。


Sample Input
1
3 3 4 20 //a b c T
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0

Sample Output
11

 1 # include <cstdio>
 2 # include <cmath>
 3 # include <iostream>
 4 # include <cstring>
 5 # include <algorithm>
 6 # include <queue>
 7 using namespace std ;
 8 
 9 int a, b, c, T;
10 int map[51][51][51] ;
11 int tur[6][3] = {-1, 0, 0, 1, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1} ;
12 struct node
13 {
14     int x,y,z,step;
15 };
16 void bfs()
17 {
18     queue<node> q ;
19     node begin={0,0,0,0};
20     map[0][0][0] = 1 ;
21     q.push(begin) ;
22     while(!q.empty())
23     {
24         node p = q.front() ;
25         q.pop() ;
26         for(int i=0; i<6; i++)
27         {
28             node temp = p ;
29             temp.x += tur[i][0] ;
30             temp.y += tur[i][1] ;
31             temp.z += tur[i][2] ;
32             if(temp.x==a-1&&temp.y==b-1&&temp.z==c-1&&temp.step<=T)
33             {
34                 printf("%d
", temp.step+1) ;
35                 return ;
36             }
37  
38             if(temp.x>=0&&temp.x<a&&temp.y>=0&&temp.y<b&&temp.z>=0&&temp.z<c&&!map[temp.x][temp.y][temp.z])
39             {
40                 map[temp.x][temp.y][temp.z] = 1 ;
41                 temp.step++ ;
42                 q.push(temp) ;
43             }
44         }
45     }
46     printf("-1
") ;
47     return ;
48 }
49 int main()
50 {
51     int t ;
52     scanf("%d", &t) ;
53     while(t--){
54         scanf("%d%d%d%d", &a, &b, &c, &T) ;
55         for(int i=0; i<a; i++)
56             for(int j=0; j<b; j++)
57                 for(int k=0; k<c; k++)
58                     scanf("%d", &map[i][j][k]) ;
59        if(map[a-1][b-1][c-1]||a+b+c-3>T)
60         {
61             printf("-1
") ;
62             continue ;
63         }
64         if(a==1&&b==1&&c==1)
65         {
66             printf("0
") ;
67             continue ;
68         }
69         bfs() ;
70     }
71     return 0 ;
72 }
View Code
原文地址:https://www.cnblogs.com/mengchunchen/p/4513298.html