1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using namespace std; 6 7 int w, h, n, s[3], t[3]; 8 char dataset[20][20]; 9 int G[200][5], vis[200][200][200], dist[200][200][200]; 10 intdeg [ 200 is ]; . 11 int DX [] = { 0 , - . 1 , . 1 , 0 , 0 }; // This contains a non-moving situation 12 is int Dy [] = { 0 , 0 , 0 , - . 1 , . 1 }; 13 is 14 inline int ID ( int A, int B, int C) 15 { 16 return (A << 16 ) | (B << . 8 ) | C; 17 } // that seemed to cock fried day data integration mode 18 is . 19 inline BOOL Conflict ( int A, int B, int A2, int B2) 20 is { 21 is return ((== A2 B2) || (A == == B && B2 A2)); 22 is // former are both moved to the same position, which is the position of both the exchange 23 is } 24 25 int BFS () 26 is { 27 Queue < int > Q; 28 q.push (ID (S [ 0 ], S [ . 1 ], S [ 2 ])); 29 dist [S [ 0 ]] [S [ . 1 ]] [S [ 2 ]] = 0 ; // dist initial value is -1, 0 is here marked 30 the while (! Q.empty ()) 31 is { 32 int U = q.front (); q.pop (); 33 is int a = (U >> 16 ) & 0xFF , B = (U >> . 8 ) & 0xFF , C = U & 0xFF ; 34 is // a species appears cock fried day data integration mode, remember !! 35 IF (a == T [ 0 ] && B == T [ . 1 ] && C == T [ 2]) Return dist [A] [B] [C]; 36 // A corresponding to the main number cnt in that it does not directly test to verify its vertical coordinate, but number 37 [ for ( int I = 0 ; I <deg [a]; I ++ ) 38 is { // deg [a] is the number cnt is the number of all possible letter a direction of movement at that point, through all possible directions 39 int A2 = G [a] [I]; 40 // letters motion results in a number cnt A2 41 is for ( int J = 0 ; J <deg [b]; J ++ ) 42 is { 43 is int B2 = G [b] [J]; 44 is // cnt number b B2 letter motion results 45 IF (Conflict (A, B, A2, B2)) Continue ; 46 is // for conflicts of movement, change direction by making the second letter yielding 47 for ( int K = 0 ; K <deg [C]; ++ K ) 48 { 49 int C2 = G [c] [K]; 50 // CNT numbered letter c movement results C2 51 is IF (Conflict (a, c, A2, C2) || Conflict (B, c, B2 , C2)) Continue ; 52 is IF (dist [A2] [B2] [C2] == - . 1 ) 53 is { 54 is dist [A2] [B2] [C2] = dist [A] [B] [C] + . 1; 55 //这是记录步数 56 q.push(ID(a2, b2, c2)); 57 } 58 } 59 } 60 } 61 } 62 return -1; 63 } 64 65 int main() { 66 //freopen("input.txt", "r", stdin); 67 while(~scanf("%d%d%d\n", &w, &h, &n) && n) 68 { 69 for(int i = 0; i < h; i++) fgets(dataset[i], 20, stdin); 70 //输入图 71 int cnt = 0, x[200], y[200], id[20][20]; 72 for(int i = 0; i < h; i++) 73 for(int j = 0; j < w; j++) 74 { 75 if(dataset[i][j] != '#')//The position of all the available actions by a number cnt 76 { 77 // S-> the CNT-> X and y should learn this is a correspondence between the 78 X [cnt] = I; y [cnt] = J; id [I] [J] = CNT; // impart unique id action position in FIG. 79 IF (islower (DataSet [I] [J])) S [DataSet [I] [J] - ' a ' ] = CNT; 80 the else IF (isupper (DataSet [I] [J])) T [DataSet [I] [J] - ' a ' ] = CNT; 81 CNT ++; // all coordinates are assigned to x and y array, cnt is the only confirmed, the data is mapped into a two data !!! 82 } // record bfs path start and end information of the array is the starting point s, t is the end of the array, 83 } 84 for ( int I = 0 ; I <CNT; I ++) // corresponding to each position 85 { 86 deg [I] = 0 ; 87 for ( int J = 0 ; J < . 5 ; J ++) // corresponding to different direction and in view of the situation does not move 88 { 89 int NX = X [I] + DX [J]; int NY = Y [I] + Dy [J]; 90 IF ! (DataSet [NX] [NY] = ' # ' ) G [I] [deg [I] ++] = ID [NX] [NY]; 91 is //G [cnt number] [direction] 92 } 93 } 94 // For each direction, all possible positions are recorded 95 IF (n-<= 2 ) {deg [CNT] = . 1 ; G [cnt] [ 0 ] = CNT; S [ 2 ] = T [ 2 ] = CNT ++ ;} 96 IF (n-<= . 1 ) {deg [CNT] = . 1 ; G [CNT] [ 0 ] = CNT; S [ . 1 ] = T [ . 1 ] = CNT ++ ;} // here seems to be the object of controlling a mobile number 97 Memset (dist, - . 1 , the sizeof (dist)); 98 printf("%d\n", bfs()); 99 } 100 return 0; 101 }
This is not my code, which is a very good code, here are a few aspects worth learning to learn:
1, the data structure design is very clever, using only ordinary array structure makes finding and established very quickly and easily transform data
2, using the map structure of this code, prepared in advance for each possible direction of the point, this can be used in the usual dfs or bfs