Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4245 Accepted Submission(s): 2750
1 /************************************************************************* 2 > File Name: code/hdu/1240.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年10月05日 星期一 18时44分48秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<iomanip> 10 #include<cstdio> 11 #include<algorithm> 12 #include<cmath> 13 #include<cstring> 14 #include<string> 15 #include<map> 16 #include<set> 17 #include<queue> 18 #include<vector> 19 #include<stack> 20 #include<cctype> 21 22 #define yn hez111qqz 23 #define j1 cute111qqz 24 #define ms(a,x) memset(a,x,sizeof(a)) 25 using namespace std; 26 const int dx6[6]={1,0,0,-1,0,0}; 27 const int dy6[6]={0,-1,1,0,0,0}; 28 const int dz6[6]={0,0,0,0,-1,1}; 29 typedef long long LL; 30 typedef double DB; 31 const int inf = 0x3f3f3f3f; 32 const int N=11; 33 char sta[30],en[30]; 34 char maze[N][N][N]; 35 bool vis[N][N][N]; 36 int n ; 37 struct node 38 { 39 int x,y,z; 40 int d; 41 42 bool ok() 43 { 44 if (x>=0&&y>=0&&z>=0&&x<n&&y<n&&z<n&&!vis[z][x][y]&&maze[z][x][y]!=‘X‘) 45 return true; 46 return false; 47 } 48 49 50 }s,tar; 51 52 int ans; 53 bool bfs() 54 { 55 vis[s.z][s.x][s.y] = true; 56 s.d = 0 ; 57 queue<node>q; 58 q.push(s); 59 while (!q.empty()) 60 { 61 node pre=q.front();q.pop(); 62 // cout<<"x:"<<pre.x<<" y:"<<pre.y<<" z:"<<pre.z<<" d:"<<pre.d<<endl; 63 64 if (pre.x==tar.x&&pre.y==tar.y&&pre.z==tar.z) 65 { 66 ans = pre.d; 67 return true; 68 } 69 70 for ( int i = 0 ; i < 6 ; i++) 71 { 72 node next; 73 next.x = pre.x + dx6[i]; 74 next.y = pre.y + dy6[i]; 75 next.z = pre.z + dz6[i]; 76 next.d = pre.d + 1; 77 if (next.ok()) 78 { 79 vis[next.z][next.x][next.y] = true; 80 q.push(next); 81 } 82 83 } 84 } 85 86 return false; 87 } 88 int main() 89 { 90 #ifndef ONLINE_JUDGE 91 freopen("in.txt","r",stdin); 92 #endif 93 while(scanf("%s",sta)!=EOF) 94 { 95 ms(vis,false); 96 scanf("%d",&n); 97 for ( int k = 0 ; k < n ; k++) 98 for ( int i = 0 ; i < n ; i++) 99 scanf("%s",maze[k][i]); 100 scanf("%d %d %d",&s.x,&s.y,&s.z); 101 scanf("%d %d %d",&tar.x,&tar.y,&tar.z); 102 scanf("%s",en); 103 if (bfs()) 104 { 105 printf("%d %d\n",n,ans); 106 } 107 else 108 { 109 puts("NO ROUTE"); 110 } 111 112 } 113 114 115 #ifndef ONLINE_JUDGE 116 fclose(stdin); 117 #endif 118 return 0; 119 }
原文:http://www.cnblogs.com/111qqz/p/4856126.html