胜利大逃亡
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 39931 Accepted Submission(s): 14002
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
问题链接:HDU1253 胜利大逃亡。
问题简述:
三维城堡(迷宫),每个点由0(可以经过)和1(墙)组成。输入测试用例数,输入每个例子的立体长宽高和限定的时间,起点是<1,1,1>,终点是<长,宽l高>,移动方向有上、下、左、右、前和后6个方向。每移动一次耗费1分钟,问能否在限定时间内最快走出。如果能则输出最短时间,不能则输出-1。
问题分析:
这个问题一看,可以说和UVA532 Dungeon Master完全相同,豪情万丈地做了拷贝来程序修改,一提交结果是“Time Limit Exceeded”,满脸困惑。多方调查研究后,终于懂得了程序简洁才是硬道理。也许因为测试数据量大,各个方面改进速度的措施都用了之后,总算是AC了。胜利大逃亡,逃出来了!
一个三维迷宫,典型的BFS问题。在BFS搜索过程中,走过的点就不必再走了,因为这次再走下去不可能比上次的步数少。
程序说明:
程序中,增加了边界使得判定条件变得简单;0和1的值对换了一下,判定条件也简单了;限定时间条件也用上了,可以减少展开的节点数量。这个程序需要处处节省时间。
把没有AC的程序也放在这里,可以看出逻辑上没有多少不同,就是不够简洁,结果是超时。
AC的C++语言程序如下:
/* HDU1253 胜利大逃亡 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int DIRECTSIZE = 6;
struct direct {
int dx;
int dy;
int dz;
} direct[DIRECTSIZE] =
{{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
const int MAXN = 50;
int cube[MAXN+2][MAXN+2][MAXN+2];
struct node {
int x, y, z, level;
};
int L, R, C, limit;
node start;
int bfs()
{
queue<node> q;
start.x = 1;
start.y = 1;
start.z = 1;
start.level = 0;
q.push(start);
cube[1][1][1] = 0;
while(!q.empty()) {
node front = q.front();
q.pop();
for(int i=0; i<DIRECTSIZE; i++) {
int nextx, nexty, nextz;
nextx = front.x + direct[i].dx;
nexty = front.y + direct[i].dy;
nextz = front.z + direct[i].dz;
if(cube[nextx][nexty][nextz]) {
if(nextx == L && nexty == R && nextz == C)
return front.level + 1;
if(front.level < limit) {
cube[nextx][nexty][nextz] = 0;
node v;
v.x = nextx;
v.y = nexty;
v.z = nextz;
v.level = front.level + 1;
q.push(v);
}
}
}
}
return -1;
}
int main()
{
int t, ans, i, j, k;
scanf("%d", &t);
while(t--) {
scanf("%d%d%d%d", &L, &R, &C, &limit);
memset(cube, 0, sizeof(cube));
for(i=1; i<=L; i++)
for(j=1; j<=R; j++)
for(k=1; k<=C; k++) {
scanf("%d", &cube[i][j][k]);
cube[i][j][k] = 1 - cube[i][j][k];
}
ans = bfs();
printf("%d\n", ans);
}
return 0;
}
没有AC的C++语言程序(Time Limit Exceeded)如下:
/* HDU1253 胜利大逃亡 */
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
const int DIRECTSIZE = 6;
struct direct {
int dx;
int dy;
int dz;
} direct[DIRECTSIZE] =
{{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
const int MAXN = 50;
int cube[MAXN][MAXN][MAXN];
struct node {
int x, y, z, level;
};
int L, R, C, limit;
node start, e2;
int ans;
void bfs()
{
queue<node> q;
ans = -1;
start.x = 0;
start.y = 0;
start.z = 0;
start.level = 0;
e2.x = L - 1;
e2.y = R - 1;
e2.z = C - 1;
q.push(start);
cube[0][0][0] = 1;
while(!q.empty()) {
node front = q.front();
q.pop();
for(int i=0; i<DIRECTSIZE; i++) {
int nextx, nexty, nextz;
nextx = front.x + direct[i].dx;
if(0 > nextx || nextx >= L)
continue;
nexty = front.y + direct[i].dy;
if(0 > nexty || nexty >= R)
continue;
nextz = front.z + direct[i].dz;
if(0 > nextz || nextz >= C)
continue;
if(nextx == e2.x && nexty == e2.y && nextz == e2.z) {
ans = front.level + 1;
break;
} else if(front.level < limit && cube[nextx][nexty][nextz] == 0) {
cube[nextx][nexty][nextz] = 1;
node v;
v.x = nextx;
v.y = nexty;
v.z = nextz;
v.level = front.level + 1;
q.push(v);
}
if(ans > 0)
break;
}
}
}
int main()
{
int t, i, j, k;
scanf("%d", &t);
while(t--) {
scanf("%d%d%d%d", &L, &R, &C, &limit);
for(i=0; i<L; i++)
for(j=0; j<R; j++)
for(k=0; k<C; k++)
scanf("%d", &cube[i][j][k]);
bfs();
cout << ans << endl;
}
return 0;
}