hdu-1253勝利大逃亡(空間座標下的BFS)

勝利大逃亡

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 61   Accepted Submission(s) : 24

Font: Times New Roman | Verdana | Georgia

Font Size:  

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勝利大逃亡(空間座標下的BFS) - NatureRan - NatureRan

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

這題沒什麼好說的,就是在空間座標下做一次BFS  1965MS險過

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct point
{
int x,y,z;
int step;
};
int dir[6][3]={
{-1,0,0},{1,0,0},{0,-1,0},
{0,1,0},{0,0,-1},{0,0,1}
};
int A,B,C,S;
int map[55][55][55];
int minstep[55][55][55];
queue <point> q;
void BFS(point s)
{
int i;
q.push(s);
point hd;
while(!q.empty())
{
hd=q.front();q.pop();
for(i=0;i<6;i++)
{
int x=hd.x+dir[i][0],y=hd.y+dir[i][1],z=hd.z+dir[i][2];
if(x>=0&&x<A&&y>=0&&y<B&&z>=0&&z<C&&map[x][y][z]==0)
{
point t;
t.x=x;t.y=y;t.z=z;
t.step=hd.step+1;
if(t.step<minstep[x][y][z])
{
minstep[x][y][z]=t.step;
q.push(t);
//cout<<x<<" "<<y<<" "<<z<<endl;
}
}
}
}
}
int main(int argc, char *argv[])
{
int t;
cin>>t;
while(t--)
{
scanf("%d%d%d%d",&A,&B,&C,&S);
int i,j,k;
memset(map,0,sizeof(map));
for(i=0;i<A;i++)
for(j=0;j<B;j++)
for(k=0;k<C;k++) {scanf("%d",&map[i][j][k]);minstep[i][j][k]=9999999;}
point start,end1;
start.x=start.y=start.z=start.step=0;
BFS(start);
int ans=minstep[A-1][B-1][C-1];
if(ans<=S) cout<<ans<<endl;
else cout<<"-1"<<endl;
//cout<<ans<<endl;
/*for(i=0;i<A;i++)
for(j=0;j<B;j++)
{
for(k=0;k<C;k++) cout<<minstep[i][j][k]<<" ";
cout<<endl;
}*/
}
return 0;
}



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