标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29504 Accepted Submission(s): 11124
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 struct node 7 { 8 int x,y,z,s; 9 } s1,s2; 10 int map[55][55][55]; 11 int a,b,c,t; 12 int f[6][3]= {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1}; //三维方向搜索 13 void BFS() 14 { 15 queue <node> q; 16 while(!q.empty()) 17 q.pop(); 18 s1.x=0; 19 s1.y=0; 20 s1.z=0; 21 s1.s=0; 22 q.push(s1); 23 map[0][0][0]=1; 24 while(!q.empty()) 25 { 26 s1=q.front(); 27 q.pop(); 28 if(s1.x==a-1&&s1.y==b-1&&s1.z==c-1&&s1.s<=t) 29 { 30 printf("%d\n",s1.s); 31 return; 32 } 33 if(s1.s>t) //剪枝,超过时间则跳出循环 34 break; 35 for(int i=0; i<6; i++) 36 { 37 s2.x=s1.x+f[i][0]; 38 s2.y=s1.y+f[i][1]; 39 s2.z=s1.z+f[i][2]; 40 if(s2.x>=0&&s2.x<a&&s2.y>=0&&s2.y<b&&s2.z>=0&&s2.z<c&&!map[s2.x][s2.y][s2.z]) //不能超过范围 41 { 42 map[s2.x][s2.y][s2.z]=1; 43 s2.s=s1.s+1; 44 q.push(s2); 45 } 46 } 47 } 48 printf("-1\n"); 49 return; 50 } 51 52 int main() 53 { 54 int T,i,j,k; 55 scanf("%d",&T); 56 while(T--) 57 { 58 scanf("%d%d%d%d",&a,&b,&c,&t); 59 for(i=0; i<a; i++) 60 for(j=0; j<b; j++) 61 for(k=0; k<c; k++) 62 scanf("%d",&map[i][j][k]); 63 BFS(); 64 } 65 return 0; 66 }
标签:
原文地址:http://www.cnblogs.com/pshw/p/4757121.html