您的位置:首页 > 其它

亡命逃窜 nyoj 523

2016-05-14 16:39 330 查看
bfs http://acm.nyist.net/JudgeOnline/problem.php?pid=523[code] #include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<queue> using namespace std; int k,a,b,c,t; struct node{ int x,y,z; int step; node(){ step=0; } }; int p[51][51][51]; int vis[52][52][52]; int check(int x,int y,int z){ if(x>=a||y>=b||z>=c||x<0||y<0||z<0){ return 0; } if(p[x][y][z]==1){ return 0; } if(vis[x][y][z]==1){ return 0; } vis[x][y][z]=1; return 1; } int bfs(){ vis[0][0][0]=1; queue<node>q; struct node t; t.x=0;t.y=0;t.z=0;t.step=0; q.push(t); while(!q.empty()){ struct node temp=q.front(); q.pop(); int xx=temp.x;int yy=temp.y;int zz=temp.z; if(xx==a-1&&yy==b-1&&zz==c-1){ return temp.step; } if(check(xx+1,yy,zz)){ t.x=xx+1;t.y=yy;t.z=zz; t.step=temp.step+1; q.push(t); } if(check(xx,yy+1,zz)){ t.x=xx;t.y=yy+1;t.z=zz; t.step=temp.step+1; q.push(t); } if(check(xx,yy,zz+1)){ t.x=xx;t.y=yy;t.z=zz+1; t.step=temp.step+1; q.push(t); } if(check(xx-1,yy,zz)){ t.x=xx-1;t.y=yy;t.z=zz; t.step=temp.step+1; q.push(t); } if(check(xx,yy-1,zz)){ t.x=xx;t.y=yy-1;t.z=zz; t.step=temp.step+1; q.push(t); } if(check(xx,yy,zz-1)){ t.x=xx;t.y=yy;t.z=zz-1; t.step=temp.step+1; q.push(t); } } return -1; } int main(){ int temp; scanf("%d",&k); while(k--){ scanf("%d%d%d%d",&a,&b,&c,&t); memset(vis,0,sizeof(vis)); for(int i=0;i<a;i++){ for(int j=0;j<b;j++){ for(int l=0;l<c;l++){ scanf("%d",&p[i][j][l]); } } } int flag=bfs(); if((flag>t)||(flag==-1)){ printf("-1\n"); } else{ printf("%d\n",flag); } } }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: