[HDU 1253] 胜利大逃亡

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1253

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