2142: 胜利大逃亡
时间限制: 1 Sec 内存限制: 128 MB
提交: 37 解决: 15
[提交][状态][讨论版][命题人:外部导入]
题目描述
Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出口就算离开城堡,如果走到出口的时候魔王刚好回来也算逃亡成功),如果可以请输出需要多少分钟才能离开,如果不能则输出-1.
输入
输入数据的第一行是一个正整数K,表明测试数据的数量.每组测试数据的第一行是四个正整数A,B,C和T(1<=A,B,C<=50,1<=T<=1000),它们分别代表城堡的大小和魔王回来的时间.然后是A块输入数据(先是第0块,然后是第1块,第2块......),每块输入数据有B行,每行有C个正整数,代表迷宫的布局,其中0代表路,1代表墙。
输出
对于每组测试数据,如果Ignatius能够在魔王回来前离开城堡,那么请输出他最少需要多少分钟,否则输出-1.
样例输入
2 6 5 5 18 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 3 7 9 0 0 1 0 1 0 0 0 0 0 0 1 0 1 1 0 1 0 1 1 0
样例输出
13 -1
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
const int maxn=50;
int G[maxn][maxn][maxn];
int inq[maxn][maxn][maxn];
struct Node
{
int x;
int y;
int z;
int t;
};
queue <Node> q;
int X[6]={1,-1,0,0,0,0};
int Y[6]={0,0,1,-1,0,0};
int Z[6]={0,0,0,0,1,-1};
int A,B,C,T,N;
int isOK(int x,int y,int z)
{
if(x<0||y<0||z<0||x>=A||y>=B||z>=C)
{
return 0;
}
if(G[x][y][z]==1||inq[x][y][z]==1)
{
return 0;
}
return 1;
}
int BFS(int A,int B,int C)
{
Node p;
p.x=0;
p.y=0;
p.z=0;
p.t=0;
q.push(p);
inq[0][0][0]=1;
while(!q.empty())
{
Node top=q.front();
q.pop();
for(int i=0;i<6;i++)
{
int newX=top.x+X[i];
int newY=top.y+Y[i];
int newZ=top.z+Z[i];
// if(newX<0||newX>=A||newY<0||newY>=B||newZ<0||newZ>=C) continue;
// if(G[newX][newY][newZ]==1) continue;
// if(inq[newX][newY][newZ]==1) continue;
int step=top.t+1;
if(isOK(newX,newY,newZ))
{
Node temp;
temp.x=newX;
temp.y=newY;
temp.z=newZ;
temp.t=top.t+1;
q.push(temp);
inq[newX][newY][newZ]=1;
if(newX==A-1&&newY==B-1&&newZ==C-1) return temp.t;
}
}
}
return -1;
}
int main()
{
int i,j,k;
scanf("%d",&N);
while(N--)
{
scanf("%d %d %d %d",&A,&B,&C,&T);
for(i=0;i<A;i++)
{
for(j=0;j<B;j++)
{
for(k=0;k<C;k++)
{
scanf("%d",&G[i][j][k]);
inq[i][j][k]=0;
}
}
}
while(q.empty()==false) q.pop();
int ans=BFS(A,B,C);
if(ans<=T) printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}
这个题目一开始总是只能得50分,然后我个人觉得实在是纳闷。我几乎是按照标准的模板写的啊,怎么可能会错呢?
后来发现,原来是数组在初始化的时候,采用了一次性初始化的方式,估计OJ平台后台编译器的标准根CodeBlocks的编译标准不一样吧。如果改成数组循环赋值,这是发现基本上没啥问题了。这点切记。