HDU 1253 勝利大逃亡


勝利大逃亡

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


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
 

Author
Ignatius.L
 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1372 1072 1240 1016 1010 
 

Statistic | Submit | Discuss | Note

簡單bfs,搜索六個方向就行了

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#define INF 1000000
using namespace std;
int map[60][60][60];
int mintime[60][60][60];
int i,j,k,a,b,c,t;
struct point{
	int x,y,z,time;
};
int dir[6][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0};
queue<point>que;
void bfs(){
	point node,now,next;
	node.x=1; node.y=1; node.z=1;
	node.time=0;
	que.push(node);
	while(!que.empty()){
		now=que.front(); que.pop();
		for(i=0;i<6;i++){
			next.x=now.x+dir[i][0];
			next.y=now.y+dir[i][1];
			next.z=now.z+dir[i][2];
			if(next.x>=1&&next.x<=a&&next.y>=1&&next.y<=b&&next.z>=1&&next.z<=c&&map[next.x][next.y][next.z]!=1){
				next.time=now.time+1;
				if(next.time<mintime[next.x][next.y][next.z]){
					mintime[next.x][next.y][next.z]=next.time;
					que.push(next);
				}
			}
		}
	}
	if(mintime[a][b][c]<INF&&mintime[a][b][c]<=t)
		printf("%d\n",mintime[a][b][c]);
	else printf("-1\n");
	return ;
}
int main(){
	int kk;
	scanf("%d",&kk);
	while(kk--){
		memset(map,0,sizeof(map));
		memset(mintime,0,sizeof(mintime));
		scanf("%d%d%d%d",&a,&b,&c,&t);
		for(i=1;i<=a;i++){
			for(j=1;j<=b;j++){
				for(k=1;k<=c;k++){
					scanf("%d",&map[i][j][k]);
					mintime[i][j][k]=INF;
				}
			}
		}
		if(a==1&&b==1&&c==1){
			printf("0\n");
			continue;
		}
		mintime[1][1][1]=0;
		bfs();
	}
	return 0;
} 





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章