Description
Each floor is marked with 0 ~ 9. A digital maze where the floor marked with 1 cannot go, marked with 2 ~ 9 The floor can jump to any position with the same number without taking the time, or the floor marked with 0 can move the distance of 1 in any direction left and right. The start and end are given, and the shortest time from the start point to the end point is obtained.
Input
The first line of each group of data is N, indicating the size, 2 <= n <= 100.
N rows in the next n rows each line N 0 ~ 9 characters, or s indicates the start point, e indicates the end point, and S and E have the same motion rules as 0. The whole map only has one s and one E.
Output
Each group of data outputs a number that occupies one row, indicating the shortest time that can be taken from the start point to the end point.
If the key cannot be reached, output "Oh no! "
Sample Input
50S100001310030000000003E03S1201010E
Sample output
4OH no!
One thing to note about the deformation of BFS is to push the corresponding numbers in case of 2-9 numbers.
1 # include <cstdio> 2 # include <queue> 3 # include <cstring> 4 # define N 105 5 using namespace STD; 6 7 int N; 8 char G [N] [N]; 9 char vis [N] [N]; 10 11 struct POS {int X, Y, D ;}; 12 queue <POS> QST [10]; 13 POS st; 14 15 // describes the relative position of neighboring points 16 const int DX [] = {0, 0, 1, -1}; 17 const int dy [] = {1,-1, 0, 0}; 18 19 int BFS () 20 {21 queue <POS> q; 22 Q. push (ST); 23 // access tag 24 vis [st. x] [st. y] = 1; 25 while (! Q. empty () {26 POS cur = Q. front (); 27 Q. pop (); 28 for (INT I = 0; I <4; ++ I) {29 POS NST; 30 // traverse the point 31 NST of the store. X = cur. X + dx [I]; 32 NST. y = cur. Y + dy [I]; 33 // determines if it is still in the range and 34 if (1 <= NST. X & NST. x <= N & 1 <= NST. Y & NST. Y <= N &&! Vis [NST. x] [NST. y]) {35 // access tag 36 vis [NST. x] [NST. y] = 1; 37 char CH = G [NST. x] [NST. y]; 38 // determines whether the end point is found. The number of returned steps is 39 if (CH = 'E') 40 return cur. d + 1; 41 // determine whether a barrier is encountered. Skip to the next loop 42 if (CH = '1') 43 continue; 44 // if it is 0 steps plus 1, push the adjacent contacts into the queue 45 if (CH = '0') 46 NST. D = cur. d + 1, Q. push (NST ); 47 // if it is 2-9, you can "transfer" the point to find the same point as the point number and press it into the queue 48 else if ('2' <= CH & Ch <= '9 ') {49 POS nnst; 50 while (! QST [CH-'0']. empty () {51 nnst = QST [CH-'0']. front (); 52 QST [CH-'0']. pop (); 53 vis [nnst. x] [nnst. y] = 1; 54 nnst. D = cur. d + 1; 55 Q. push (nnst); 56} 57} 58} 59} 60} 61 Return-1; 62} 63 64 int main () 65 {66 while (~ Scanf ("% d", & N) {67 pos tmp; 68 // clear the queue 69 for (INT I = 2; I <= 9; I ++) 70 while (! QST [I]. empty () 71 QST [I]. pop (); 72 for (INT I = 1; I <= N; I ++) {73 scanf ("% s", G [I] + 1 ); 74 memset (vis [I] + 1, 0, sizeof (char) * n); 75 for (Int J = 1; j <= N; j ++) {76 char CH = G [I] [J]; 77 // press the hop position into the queue 78 If ('2' <= CH & Ch <= '9 ') {79 TMP. X = I, TMP. y = J; 80 QST [CH-'0']. push (TMP); 81} 82 // record initial position 83 else if (CH = 's') {84 St. X = I, St. y = J; 85 St. D = 0; 86} 87} 88} 89 int ans = BFS (); 90 if (ANS =-1) 91 printf ("Oh no! \ N "); 92 else93 printf (" % d \ n ", ANS); 94} 95 return 0; 96}