HDU 1253 胜利大逃亡 NYOJ 523【BFS】

时间:2021-10-24 15:51:11

胜利大逃亡

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 24608    Accepted Submission(s): 9427
Problem Description
Ignatius被魔王抓走了,有一天魔王出差去了,这但是Ignatius逃亡的好机会.



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



HDU 1253 胜利大逃亡 NYOJ 523【BFS】

 
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
 
Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
10887454 2014-06-18 09:50:19 Time Limit Exceeded

pid=1253">1253

2000MS 944K 1328 B G++

user=Changmu">长木

10887453 2014-06-18 09:49:37 Accepted 1253 1484MS 996K 1328 B C++ 长木

同一段代码提交时选用不同的编译器结果区别这么大。

#include <cstdio>
#include <queue>
using std::queue;
int a, b, c, times;
int map[52][52][52];
bool vis[52][52][52];
const int mov[][3] = {
0, 0, 1, 0, 1, 0,
1, 0, 0, 0, 0, -1,
0, -1, 0, -1, 0, 0
};
struct Node{
int x, y, z, steps;
};
queue<Node> Q; bool check(Node k){
if(k.x < 0 || k.y < 0 || k.z < 0 ||
k.x >= a || k.y >= b || k.z >= c) return 0;
if(vis[k.x][k.y][k.z] || map[k.x][k.y][k.z])
return 0;
return 1;
} int BFS(){
while(!Q.empty()) Q.pop();
Node temp, now = {0};
vis[0][0][0] = 1;
if(a == b && a == c && a == 1) return 0;
Q.push(now);
while(!Q.empty()){
now = Q.front();
Q.pop(); if(now.steps == times) continue; for(int i = 0; i < 6; ++i){
temp = now; ++temp.steps;
temp.x += mov[i][0];
temp.y += mov[i][1];
temp.z += mov[i][2];
if(check(temp)){
if(temp.x == a-1 && temp.y == b-1 && temp.z == c-1)
return temp.steps;
vis[temp.x][temp.y][temp.z] = 1;
Q.push(temp);
}
}
}
return -1;
} int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%d%d%d%d", &a, &b, &c, ×);
for(int i = 0; i < a; ++i)
for(int j = 0; j < b; ++j)
for(int k = 0; k < c; ++k){
scanf("%d", &map[i][j][k]);
vis[i][j][k] = 0;
} printf("%d\n", BFS());
}
return 0;
}