T4870 flood (sliker. cpp/c/pas) 1000 MS 64 MB, t4870sliker. cpp
Description
It rained for a few days, but it still did not stop. Tuhao CCY just returned from the field after earning 1e yuan, knowing that soon except his villa, other places will be flooded.
The city where CCY is located can be represented by a map of N * M (N, M <= 50). There are five symbols on the map: ". * x d s ". "X" indicates that stone, water and people cannot pass through it. "." Indicates the plain, CCY and flood can go through. "*" Indicates the place where the flood starts (there may be multiple places where the flood begins ). "D" indicates the villa of CCY. "S" indicates the current position of CCY.
CCY can be moved to adjacent locations every minute, and the flood will flood the adjacent non-existing land (from the already drowned land) after CCY moves ).
Find the minimum time for CCY to return to the villa. If Cong Ge cannot return home, it is likely to be drowned, then he will worship the gold god up RP to call the helicopter, so the output "ORZ hzwer !!!".
Input/Output Format
Input Format:
3 3D .*
.... S.
Output Format:
3
Input and Output sample
Input example #1:
3 3D.*…..S
Output sample #1:
ORZ hzwer!!!
Input example #2:
3 6D…*..X.X..….S.
Output sample #2:
6
It is very easy to search widely. Just simulate the problem according to the requirements of the question.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 using namespace std; 7 int n, m; 8 int xx [7] = {-1, + 1,0, 0}; 9 int yy [7] = {0,-1, + 1 }; 10 struct peo 11 {12 int juli; // 13 int x, y; 14 int step; 15} now, nxt; 16 int map [201] [201]; 17 int bgx, bgy, homex, homey; 18 int vis [201] [201]; // a place flooded with water, make sure that both map and vis are used to determine 19 int ans = 438438; 20 int watercishu = 1; 21 int flag. = 0; 22 int vis2 [201] [201]; 23 int calca (int xxx, int yyy) 24 {25 return max (xxx, homex)-min (xxx, homex) + max (yyy, homey)-min (yyy, homey); 26} 27 void init () 28 {29 scanf ("% d", & n, & m); 30 for (int I = 1; I <= n; I ++) 31 for (int j = 1; j <= m; j ++) 32 {33 char p; 34 cin> p; 35 if (p = '. ') 36 map [I] [j] = 0; // you can use 37 else if (p = 'X') 38 map [I] [j] = 1; // none can pass through 39 else if (p ='s ') 40 {map [I] [j] = 2; // the current position of the person is 41 bgx = I; bgy = j;} 42 else if (p = '*') 43 map [I] [j] = 3, vis [I] [j] = 1; // location 44 else if (p = 'D') 45 {46 map [I] [j] = 4; // home 47 homex = I; 48 homey = j; 49} 50 51} 52} 53 void ex () 54 {55 int flag = 0; 56 for (int I = 1; I <= n; I ++) 57 {58 for (int j = 1; j <= m; j ++) 59 {60 if (vis [I] [j] = watercishu) 61 {62 for (int k = 0; k <4; k ++) 63 {64 int wx = I + xx [k]; 65 int wy = j + yy [k]; 66 if (vis [wx] [wy] = 0 & map [wx] [wy]! = 1 & map [wx] [wy]! = 4 & wx> = 1 & wx <= n & wy> = 1 & wy <= m) 67 {68 vis [wx] [wy] = vis [I] [j] + 1; 69} 70} 71} 72} 73} 74 watercishu ++; 75} 76 void bfs () 77 {78 queue <peo> q; 79 now. x = bgx; now. y = bgy; now. step = 0; now. juli = calca (bgx, bgy); 80 q. push (now); 81 int last = 0; // records the number of human steps during the last flood extension 82 while (q. size ()! = 0) 83 {84 peo p = q. front (); 85 if (vis [p. x] [p. y]! = 0) 86 {87 q. pop (); 88 continue; 89} 90 if (p. juli = 0) 91 {92 printf ("% d", p. step); 93 flag = 1; 94 return; 95} 96 97 q. pop (); 98 if (p. step> last) 99 {100 ex (); // flood expansion 101 last = p. step; 102} 103 if (vis [p. x] [p. y]! = 0) 104 {105 continue; 106} 107 for (int I = 0; I <4; I ++) 108 {109 int wx = p. x + xx [I]; 110 int wy = p. y + yy [I]; 111 if (vis2 [wx] [wy] = 0 & vis [wx] [wy] = 0 & map [wx] [wy]! = 1 & wx> = 1 & wx <= n & wy> = 1 & wy <= m) 112 {113 vis2 [wx] [wy] = 1; 114 nxt. x = wx; 115 nxt. y = wy; 116 nxt. step = p. step + 1; 117 nxt. juli = calca (wx, wy); 118 q. push (nxt); 119} 120} 121 122} 123} 124 int main () 125 {126 // freopen ("sliker. in "," r ", stdin); 127 // freopen (" sliker. out "," w ", stdout); 128 init (); 129 bfs (); 130 if (flag = 0) 131 printf (" ORZ hzwer !!! "); 132 return 0; 133}