代码超时:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
const int maxx=1e2+10;
const int inf=0x3f3f3f3f;
int dx[6]={1,0,-1,0,0,0};
int dy[6]={0,-1,0,1,0,0};
int dz[6]={0,0,0,0,1,-1};
int n,m,k;
int time;
int e[maxx][maxx][maxx];
int vis[maxx][maxx][maxx];
int st,et;
struct node{
int x,y,z;
int time;
bool friend operator<(const node a,const node b){
return a.time<b.time;
}
};
int check(int x,int y,int z){
if(x<n&&x>=0&&y<m&&y>=0&&z>=0&&z<k&&vis[z][x][y]==0&&e[z][x][y]!=1){
return 1;
}else{
return 0;
}
}
int bfs(int x,int y,int z){
priority_queue<node>q;
node a;
a.x=x;
a.y=y;
a.z=z;
a.time=0;
q.push(a);
vis[z][x][y]=1;
while(!q.empty()){
node u=q.top();
q.pop();
if(u.x==n-1&&u.y==m-1&&u.z==k-1){
return u.time;
}
for(int i=0;i<6;i++){
node t;
t.x=u.x+dx[i];
t.y=u.y+dy[i];
t.z=u.z+dz[i];
t.time=u.time+1;
if(check(t.x,t.y,t.z)){
q.push(t);
vis[t.z][t.x][t.y]=1;
}
}
}
return inf;
}
int main(){
int t;
cin>>t;
while(t--){
scanf("%d %d %d %d",&k,&n,&m,&time);
if(n==0&&m==0&&time==0)break;
memset(vis,0,sizeof(vis));
for(int x=0;x<k;x++){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>e[x][i][j];
}
}
}
vis[0][0][0]=1;
int flag=bfs(0,0,0);
if(flag<=time){
cout<<flag<<endl;
}else{
cout<<"-1"<<endl;
}
}
return 0;
}
HDU1253
最新推荐文章于 2021-10-21 18:26:48 发布