Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8683 Accepted Submission(s): 4172
1 /************************************************************************* 2 > File Name: code/hdu/1072.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年10月03日 星期六 23时23分40秒 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 dx4[4]={1,0,0,-1}; 27 const int dy4[4]={0,-1,1,0}; 28 typedef long long LL; 29 typedef double DB; 30 const int inf = 0x3f3f3f3f; 31 const int N=10; 32 int a[N][N]; 33 bool vis[N][N][N]; 34 int n,m; 35 struct node{ 36 int x,y; 37 int t; 38 int d; 39 bool ok() 40 { 41 if (x>=0&&y>=0&&x<n&&y<m&&!vis[x][y][t]&&a[x][y]>0&&t>0) return true; 42 return false; 43 } 44 45 bool escape() 46 { 47 if (a[x][y]==3) 48 return true; 49 return false; 50 } 51 bool rbomb() 52 { 53 if (a[x][y]==4) return true; 54 return false; 55 } 56 void mark() 57 { 58 vis[x][y][t] = true; 59 } 60 }s; 61 62 bool bfs() 63 { 64 queue<node>q; 65 q.push(s); 66 int step = 0; 67 while (!q.empty()&&step<n*m*n*m) 68 { 69 step++; 70 node pre = q.front();q.pop(); 71 // cout<<"x:"<<pre.x<<" y:"<<" a[x][y]:"<<a[pre.x][pre.y]<<" t:"<<pre.t<<" d:"<<pre.d<<endl; 72 if (pre.escape()) 73 { 74 printf("%d\n",pre.d); 75 return true; 76 } 77 78 for ( int i = 0 ; i < 4 ; i++) 79 { 80 node next; 81 next.x = pre.x + dx4[i]; 82 next.y = pre.y + dy4[i]; 83 next.d = pre.d + 1; 84 next.t = pre.t - 1; 85 if (!next.ok()) continue; 86 if (next.rbomb()) 87 { 88 next.t = 6; 89 } 90 next.mark(); 91 q.push(next); 92 93 94 } 95 } 96 return false; 97 98 } 99 int main() 100 { 101 #ifndef ONLINE_JUDGE 102 freopen("in.txt","r",stdin); 103 #endif 104 105 int T; 106 scanf("%d",&T); 107 while (T--) 108 { 109 ms(vis,false); 110 scanf("%d %d",&n,&m); 111 for ( int i = 0 ; i < n; i++) 112 for ( int j = 0 ; j < m ; j++) 113 { 114 scanf("%d",&a[i][j]); 115 if (a[i][j]==2) 116 { 117 s.x = i; 118 s.y = j; 119 s.d = 0; 120 s.t = 6; 121 vis[s.x][s.y][s.t] = true; 122 } 123 } 124 if(!bfs()) 125 { 126 puts("-1"); 127 } 128 129 } 130 131 132 #ifndef ONLINE_JUDGE 133 fclose(stdin); 134 #endif 135 return 0; 136 }
原文:http://www.cnblogs.com/111qqz/p/4854757.html