Kolstad and Schrijvers
Farmer John went crazy and created a huge maze of fences out in a field. Happily, he left out two fence segments on the edges, and thus created two "exits" for the maze. Even more happily, the maze he created by this overfencing experience is a `perfect' maze: you can find a way out of the maze from any point inside it.
Given W (1 <= W <= 38), the width of the maze; H (1 <= H <= 100), the height of the maze; 2*H+1 lines with width 2*W+1 characters that represent the maze in a format like that shown later - then calculate the number of steps required to exit the maze from the `worst' point in the maze (the point that is `farther' from either exit even when walking optimally to the closest exit). Of course, cows walk only parallel or perpendicular to the x-y axes; they do not walk on a diagonal. Each move to a new square counts as a single unit of distance (including the move "out" of the maze.
Here's what one particular W=5, H=3 maze looks like:
+-+-+-+-+-+ | | +-+ +-+ + + | | | | + +-+-+ + + | | | +-+ +-+-+-+
Fenceposts appear only in odd numbered rows and and odd numbered columns (as in the example). The format should be obvious and self explanatory. Each maze has exactly two blank walls on the outside for exiting.
PROGRAM NAME: maze1
INPUT FORMAT
Line 1: | W and H, space separated |
Lines 2 through 2*H+2: | 2*W+1 characters that represent the maze |
SAMPLE INPUT (file maze1.in)
5 3 +-+-+-+-+-+ | | +-+ +-+ + + | | | | + +-+-+ + + | | | +-+ +-+-+-+
OUTPUT FORMAT
A single integer on a single output line. The integer specifies the minimal number of steps that guarantee a cow can exit the maze from any possible point inside the maze.
SAMPLE OUTPUT (file maze1.out)
9
The lower left-hand corner is *nine* steps from the closest exit.
思路:BFS 从两个出口开始找,不得不说,C++的queue实在太强大了
Executing... Test 1: TEST OK [0.000 secs, 3448 KB] Test 2: TEST OK [0.000 secs, 3448 KB] Test 3: TEST OK [0.000 secs, 3448 KB] Test 4: TEST OK [0.000 secs, 3448 KB] Test 5: TEST OK [0.000 secs, 3448 KB] Test 6: TEST OK [0.000 secs, 3448 KB] Test 7: TEST OK [0.000 secs, 3448 KB] Test 8: TEST OK [0.000 secs, 3448 KB] Test 9: TEST OK [0.000 secs, 3448 KB] Test 10: TEST OK [0.000 secs, 3448 KB] All tests OK.
1 /* 2 ID:wuhuaju2 3 PROG:maze1 4 LANG:C++ 5 */ 6 #include <queue> 7 #include <cstdio> 8 #include <iostream> 9 #include <cstdlib> 10 #include <algorithm> 11 #include <cstring> 12 #include <string> 13 using namespace std; 14 15 struct qq 16 { 17 int x,y; 18 } s,cpy; 19 20 queue<qq> q; 21 22 const int dx[]={0,0,-1,1}; 23 const int dy[]={1,-1,0,0}; 24 int step,l,x,y,m,n,cnt; 25 int ax[3],ay[3]; 26 char map[210][210]; 27 bool f[210][210]; 28 29 void close() 30 { 31 fclose(stdin); 32 fclose(stdout); 33 exit(0); 34 } 35 36 bool judge() 37 { 38 if (x>=0 && x<n && y>=0 && y<m) 39 return true; 40 return false; 41 } 42 43 void work() 44 { 45 // close(); 46 s.x=ax[1]; s.y=ay[1]; 47 q.push(s); 48 f[s.x][s.y]=true; 49 s.x=ax[2]; s.y=ay[2]; 50 q.push(s); 51 f[s.x][s.y]=true; 52 step=0; 53 while (!q.empty()) 54 { 55 step++; 56 // printf("step:%d\n",step); 57 l=q.size(); 58 for (int i=1;i<=l;i++) 59 { 60 s=q.front(); 61 q.pop(); 62 // printf("s.x:%d s.y:%d \n",s.x,s.y); 63 for (int j=0;j<4;j++) 64 { 65 x=s.x+dx[j]; y=s.y+dy[j]; 66 // printf("x:%d y:%d \n",x,y); 67 if (judge()) 68 if (map[x][y]==' ' && f[x][y]==false) 69 { 70 cpy.x=x; cpy.y=y; 71 f[x][y]=true; 72 q.push(cpy); 73 } 74 } 75 } 76 // cout<<"#########################"<<endl; 77 } 78 // printf("STEPPPPPPPPPPPPPPPPPPP:%d\n",step/2); 79 cout<<step/2<<endl; 80 } 81 82 void init () 83 { 84 freopen("maze1.in","r",stdin); 85 freopen("maze1.out","w",stdout); 86 scanf("%d%d",&m,&n); 87 n=n*2+1; 88 m=m*2+1; 89 char str[210]; 90 gets(str); 91 for (int i=0;i<n;i++) 92 fgets(map[i],210,stdin); 93 /* 94 for (int i=0;i<n;i++) 95 { 96 for (int j=0;j<m;j++) 97 cout<<map[i][j]; 98 cout<<endl; 99 } 100 */ 101 cnt=1; 102 for (int i=0;i<m;i++) 103 { 104 if (map[0][i]==' ') 105 { 106 ax[cnt]=0; 107 ay[cnt]=i; 108 cnt++; 109 } 110 if (map[n-1][i]==' ') 111 { 112 ax[cnt]=n-1; 113 ay[cnt]=i; 114 cnt++; 115 } 116 } 117 for (int i=0;i<n;i++) 118 { 119 if (map[i][0]==' ') 120 { 121 ax[cnt]=i; 122 ay[cnt]=0; 123 cnt++; 124 } 125 if (map[i][m-1]==' ') 126 { 127 ax[cnt]=i; 128 ay[cnt]=m-1; 129 cnt++; 130 } 131 } 132 // printf("ax[1]:%d ay[1]:%d \n ax[2]:%d ay[2]:%d \n",ax[1],ay[1],ax[2],ay[2]); 133 } 134 135 int main () 136 { 137 init(); 138 work(); 139 close(); 140 return 0; 141 }