hdu--1253--胜利大逃亡(bfs)

胜利大逃亡

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 37531    Accepted Submission(s): 13235


Problem Description
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

 

 1 /*
 2     Npme: hdu--1253--胜利大逃亡
 3     Copyright: ©2017 日天大帝
 4     Author: 日天大帝 
 5     Date: 28/04/17 19:31
 6     Description: bfs,错了几次,没剪枝TLE了 
 7 */
 8 #include<queue> 
 9 #include<iostream>
10 #include<cstring>
11 using namespace std;
12 struct node{
13     int x,y,z,steps;
14     bool operator <(const node &a)const {
15         return steps>a.steps;
16     }
17 };
18 int bfs();
19 const int MAX = 51;
20 int map[MAX][MAX][MAX];
21 int a,b,c,t;
22 int dir[6][3] = {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};
23 int main(){
24 //    freopen("in.txt","r",stdin);
25     ios::sync_with_stdio(false);
26     int k;cin>>k;
27     while(k--) {
28         memset(map,0,sizeof(map));
29         cin>>a>>b>>c>>t;
30         for(int i=0; i<a; ++i){
31             for(int j=0; j<b; ++j){
32                 for(int u=0; u<c; ++u){
33                     cin>>map[i][j][u];
34                 }
35             }
36         }
37         if(a+b+c - 3 > t){//剪枝 ,起点与终点间最短路径大于时间限度 
38             cout<<-1<<endl;
39             continue;
40         }
41         cout<<bfs()<<endl;
42     }
43     return 0;
44 }
45 int walk(node p) {
46     if(p.x <0 ||p.y<0||p.z<0||p.x>=b||p.z>=a||p.y>=c || map[p.z][p.x][p.y] == 1)return 1;
47     return 0;
48 }
49 int bfs(){
50     priority_queue<node> q;
51     node s,p,temp;
52     s.x = s.y = s.z = s.steps = 0;
53     map[0][0][0] = 1;//
54     if(s.x == b-1 && s.z == a-1 && s.y == c-1)return 0;//剪枝 
55     q.push(s);
56     while(!q.empty()){
57         temp = q.top();q.pop();
58         for(int i=0; i<6; ++i){
59             p = temp;
60             p.z += dir[i][0];
61             p.x += dir[i][1];
62             p.y += dir[i][2];
63             if(walk(p))continue;
64             if(p.x == b-1 && p.y == c-1 && p.z == a-1)return p.steps+1;
65             if(p.steps > t)continue;
66             p.steps++;
67             map[p.z][p.x][p.y] = 1;//
68             q.push(p);
69         }
70     }
71     return -1;
72 }

 

posted @ 2017-04-28 20:22  朤尧  阅读(279)  评论(0编辑  收藏  举报