标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28023 Accepted Submission(s): 10632
#include<stdio.h> #include<string.h> #include<queue> #define MAX 55 using namespace std; int map[MAX][MAX][MAX]; int vis[MAX][MAX][MAX]; int a,b,c,t; struct node { int x,y,z; int step; }; void bfs(int x1,int y1,int z1,int x2,int y2,int z2) { int i,j,k,ok=0; int move[6][3]={0,0,1,0,0,-1,1,0,0,-1,0,0,0,1,0,0,-1,0};//要对六个方向进行搜索 queue<node>q; node beg,end; beg.x=x1; beg.y=y1; beg.z=z1; beg.step=0; q.push(beg); while(!q.empty()) { end=q.front(); q.pop(); if(end.x==x2&&end.y==y2&&end.z==z2) { ok=1; break; } for(i=0;i<6;i++) { beg.x=end.x+move[i][0]; beg.y=end.y+move[i][1]; beg.z=end.z+move[i][2]; if(!vis[beg.x][beg.y][beg.z]&&0<=beg.x&&beg.x<a&&0<=beg.y&&beg.y<b&&0<=beg.z&&beg.z<c&&map[beg.x][beg.y][beg.z]==0) { vis[beg.x][beg.y][beg.z]=1; beg.step=end.step+1; map[beg.x][beg.y][beg.z]==1; q.push(beg); } } } if(ok) { if(end.step<=t) printf("%d\n",end.step); else printf("-1\n"); } else printf("-1\n"); } int main() { int k,j,i,n; 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",&map[i][j][k]); } } } memset(vis,0,sizeof(vis)); bfs(0,0,0,a-1,b-1,c-1); } return 0; } //1253
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4591378.html