您的位置:首页 > 其它

HDU 1253 胜利大逃亡

2014-08-29 20:31 337 查看

胜利大逃亡

Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 1253
64-bit integer IO format: %I64d Java class name: Main

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

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int dir[6][3] = {0,0,-1,0,0,1,0,-1,0,0,1,0,-1,0,0,1,0,0};
struct node {
int x,y,z,step;
node(int a = 0,int b = 0,int c = 0,int d = 0) {
x = a;
y = b;
z = c;
step = d;
}
};
bool mp[60][60][60],vis[60][60][60];
int A,B,C,T;
node q[500000];
int head,tail;
int bfs() {
int i,j,k,x,y,z;
node temp(1,1,1,0);
head = tail = 0;
q[tail++] = temp;
vis[temp.x][temp.y][temp.z] = true;
while(head < tail) {
node now = q[head++];
if(now.x == A && now.y == B && now.z == C)
return now.step;
for(int i = 0; i < 6; i++) {
x = now.x+dir[i][0];
y = now.y+dir[i][1];
z = now.z+dir[i][2];
if(!mp[x][y][z] && !vis[x][y][z]) {
vis[x][y][z] = true;
q[tail++] = node(x,y,z,now.step+1);
}
}
}
return -1;
}
int main() {
int t,i,j,k,tmp;
scanf("%d",&t);
while(t--) {
scanf("%d %d %d %d",&A,&B,&C,&T);
memset(vis,false,sizeof(vis));
memset(mp,true,sizeof(mp));
for(i = 1; i <= A; i++) {
for(j = 1; j <= B; j++) {
for(k = 1; k <= C; k++) {
scanf("%d",&tmp);
mp[i][j][k] = tmp;
}
}
}
tmp = bfs();
printf("%d\n",tmp < T?tmp:-1);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: