Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21542 Accepted Submission(s): 8479
一句话,走到哪,把周围OK的推到queue 里面。
1 3 3 4 20 0 1 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 0 0 1 0 1 1 1 0 0 0 0 0 1 1 0 0 1 1 0
11#include <iostream> #include <queue> #include <fstream> #include <string.h> using namespace std; class walkmanT { public: int x,y,z,n; }; int map[55][55][55]; //int step[6][3]={{1£¬0£¬0},{-1£¬0£¬0},{0£¬1£¬0},{0£¬-1£¬0},{0£¬0£¬1},{0£¬0£¬-1}}; int step[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}; int howManySteps(int A,int B,int C,int max_time) { queue<walkmanT> que; walkmanT start,next,temp; start.x=1;start.y=1;start.z=1;start.n=0; que.push(start); map[1][1][1]=1; while(que.size()!=0) { next=que.front(); que.pop(); if(next.x==A&&next.y==B&&next.z==C) { return next.n; } if(next.n>=max_time)return -1; for(int i=0;i<6;i++) { temp.x=next.x+step[i][0]; temp.y=next.y+step[i][1]; temp.z=next.z+step[i][2]; if(map[temp.x][temp.y][temp.z]==0) { temp.n=next.n+1; que.push(temp); map[temp.x][temp.y][temp.z]=1; } } } return -1; } int main() { int n,A,B,C,max_time,z; scanf("%d",&n); for(int z=0;z<n;z++){ scanf("%d%d%d%d",&A,&B,&C,&max_time); for(int i=0;i<=A+1;i++) { for(int j=0;j<=B+1;j++) { for(int k=0;k<=C+1;k++) { if(i>=1&&i<=A&&j>=1&&j<=B&&k>=1&&k<=C) { scanf("%d",&map[i][j][k]); } else{map[i][j][k]=1;} } } } cout<<howManySteps(A,B,C,max_time)<<endl; } }