HDU 1253 胜利大逃亡

题目:

Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.

魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出口就算离开城堡,如果走到出口的时候魔王刚好回来也算逃亡成功),如果可以请输出需要多少分钟才能离开,如果不能则输出-1.

Input输入数据的第一行是一个正整数K,表明测试数据的数量.每组测试数据的第一行是四个正整数A,B,C和T(1<=A,B,C<=50,1<=T<=1000),它们分别代表城堡的大小和魔王回来的时间.然后是A块输入数据(先是第0块,然后是第1块,第2块......),每块输入数据有B行,每行有C个正整数,代表迷宫的布局,其中0代表路,1代表墙.(如果对输入描述不清楚,可以参考Sample Input中的迷宫描述,它表示的就是上图中的迷宫)

特别注意:本题的测试数据非常大,请使用scanf输入,我不能保证使用cin能不超时.在本OJ上请使用Visual C++提交.
Output对于每组测试数据,如果Ignatius能够在魔王回来前离开城堡,那么请输出他最少需要多少分钟,否则输出-1.
Sample Input
1
3 3 4 20
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
题意描述:
输入城堡的长宽高和大魔王回来的时间T以及城堡的情况
如果能在时间T内走出城堡,输出最短时间,否则输出-1
解题思路:
使用三位数组,再对其进行广搜即可。
代码实现:
 1 #include<stdio.h>
 2 int h[60][60][60];
 3 struct n
 4 {
 5     int x,y,z,s;
 6 };
 7 struct n q[126000];
 8 int main()
 9 {
10     int T,a,b,c,t,head,tail,i,j,k,flag,tx,ty,tz;
11     int next[6][3]={{0,1,0},{0,-1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}}; 
12     scanf("%d",&T);
13     while(T--)
14     {
15         scanf("%d%d%d%d",&a,&b,&c,&t);
16         for(i=0;i<a;i++)
17             for(j=0;j<b;j++)
18                 for(k=0;k<c;k++)
19                     scanf("%d",&h[i][j][k]);
20         if(a==1&&b==1&&c==1)
21         {
22             printf("0
");
23             continue;
24         }
25         head=1;
26         tail=1;
27         q[tail].x=0;
28         q[tail].y=0;
29         q[tail].z=0;
30         q[tail].s=0;
31         tail++;
32         h[0][0][0]=1;
33         flag=0;
34         while(head<tail)
35         {
36             if(q[head].s==t)
37                 break;
38             for(i=0;i<6;i++)
39             {
40                 tx=q[head].x+next[i][0];
41                 ty=q[head].y+next[i][1];
42                 tz=q[head].z+next[i][2];//或者是tx 
43                 
44                 if(tx < 0 || tx >= a ||ty <0 ||ty >= b||tz < 0||tz >= c)
45                     continue;
46                 if(h[tx][ty][tz]==0)
47                 {
48                     if(tx==a-1 && ty==b-1 && tz== c-1) 
49                     h[tx][ty][tz]=0;
50                     else
51                     h[tx][ty][tz]=1;//终点的初始化问题 
52                     
53                     q[tail].x=tx;
54                     q[tail].y=ty;
55                     q[tail].z=tz;
56                     q[tail].s=q[head].s+1;
57                     tail++;//队尾移动 
58                 } 
59                 if(tx==a-1 && ty==b-1 && tz== c-1&& h[tx][ty][tz]==0)
60                 {
61                     flag=1;
62                     break;
63                 }
64             }
65             if(flag)
66                 break;
67             head++;
68         }
69         if(flag)
70         printf("%d
",q[tail-1].s);
71         else
72         printf("-1
");
73     }
74     return 0;
75 } 
 1 #include<cstdio>
 2 #include<queue>
 3 using namespace std;
 4 
 5 const int maxn = 60;
 6 struct NODE{
 7     int x, y, z, s;
 8 };
 9 bool map[maxn][maxn][maxn];
10 int a, b, c, t, ans;
11 int bfs();
12 
13 int main()
14 {
15     int T;
16     scanf("%d", &T);
17     
18     while(T --){
19         scanf("%d%d%d%d", &a, &b, &c, &t);
20         for(int i = 0; i < a; i++){
21             for(int j = 0; j < b; j++){
22                 for(int k = 0; k < c; k++){
23                     scanf("%d", &map[i][j][k]);
24                 }
25             }
26         }
27         
28         ans = 0;
29         if(bfs())    
30             printf("%d
", ans);//注意输出换行 
31         else
32             printf("-1
");
33     }
34     return 0;
35 } 
36 
37 int bfs(){
38     struct NODE head;    
39     head.x = head.y = head.z = head.s = 0;
40     
41     queue<struct NODE> q;
42     q.push(head);
43     
44     int nexts[6][3] = {0,-1,0,0,1,0, 1,0,0,-1,0,0, 0,0,1,0,0,-1};
45     
46     while(!q.empty()){
47         struct NODE tmp = q.front();
48         q.pop();
49         
50         for(int k = 0; k < 6; k++){
51             int tx = tmp.x + nexts[k][0];
52             int ty = tmp.y + nexts[k][1];
53             int tz = tmp.z + nexts[k][2];
54             
55             if(tx < 0 || tx >= a || ty < 0 || ty >= b || tz < 0 || tz >= c || map[tx][ty][tz] == 1 || tmp.s + 1 > t)
56                 continue;//少了个等号,注意细节 
57             
58             if(tx == a - 1 && ty == b - 1 && tz == c - 1 && tmp.s + 1 <= t){
59                 ans = tmp.s + 1;
60                 return 1;
61             }
62 
63             map[tx][ty][tz] = 1;
64             struct NODE tmp1;
65             tmp1.x = tx;
66             tmp1.y = ty;
67             tmp1.z = tz;
68             tmp1.s = tmp.s+1;
69             
70             q.push(tmp1);
71         }
72     }
73     return 0;
74 }

易错分析:

1、注意广搜时队尾的移动

原文地址:https://www.cnblogs.com/wenzhixin/p/7276775.html