勝利大逃亡
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
魔王住在一個城堡裏,城堡是一個A*B*C的立方體,可以被表示成A個B*C的矩陣,剛開始Ignatius被關在(0,0,0)的位置,離開城堡的門在(A-1,B-1,C-1)的位置,現在知道魔王將在T分鐘後回到城堡,Ignatius每分鐘能從一個座標走到相鄰的六個座標中的其中一個.現在給你城堡的地圖,請你計算出Ignatius能否在魔王回來前離開城堡(只要走到出口就算離開城堡,如果走到出口的時候魔王剛好回來也算逃亡成功),如果可以請輸出需要多少分鐘才能離開,如果不能則輸出-1.
Input
特別注意:本題的測試數據非常大,請使用scanf輸入,我不能保證使用cin能不超時.在本OJ上請使用Visual C++提交.
Output
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;
}