tag: blog OS Io for 2014 ar time
/* BFS is carried out at the same time by traversing the bytes of the BFS width to the person and the fire. Note that the people should be fired first, that is, if the grid is on fire before the person reaches the grid, they should not leave, when the last person reaches the border, there is no way to go. Impossible !!!!!!!!!!!! ------------------------------------------------------------------------- BFS twice: bfs1 (); fire BFS: first bfs1 followed by BFS ();;;;;;;;;;;;;;;;;;;;;;;;;;;; or calculate the time when the grid is on fire (INT I = 0; I <r; I ++). Because there are more than one fire grid, the for loop {for (Int J = 0; j <C; j ++) {If (G [I] [J] = 'F') enters the queue {q [rear]. X = J; Q [rear]. y = I; Q [rear]. step = 0; Step 0 Q [rear ++]. flag = 1; mark as fire lattice }}------------------------------------------------------------------------------- Q [rear]. X = FX; a person (starting point) enters the queue Q [rear]. y = FY; Q [rear]. step = 0; Q [rear ++]. flag = 0; --------------------------------------------------------------------------------- if (Q [Front]. flag) fire point extension {for (INT I = 0; I <4; I ++) each extension has four directions {xx = Q [Front]. X + step_x [I]; update the extension point location YY = Q [Front]. Y + step_y [I]; if (XX <0 | XX> = c | YY <0 | YY> = r | G [YY] [XX] = '#' | G [YY] [XX] = 'F ') if a fire occurs across the border, the obstacle continues; G [YY] [XX] = 'F'; the fire spreads to Q [rear]. X = xx; the extension point is changed to the current point Q [rear]. y = YY; Q [rear]. flag = 1; mark as fire Q [rear ++]. step = Q [Front]. step + 1; Add 1} each time for (INT I = 0; I <4; I ++) extension {xx = Q [Front]. X + step_x [I]; YY = Q [Front]. Y + step_y [I]; if (vis [YY] [XX] | G [YY] [XX] = 'F' | G [YY] [XX] = '#') continue; if (XX <0 | XX> = c | YY <0 | YY> = r) return Q [Front]. step + 1; vis [YY] [XX] = 1; Q [rear]. X = xx; Q [rear]. y = YY; Q [rear]. flag = 0; Q [rear]. step = Q [Front]. step + 1; ++ rear; optional */# include <iostream> # include <cstdio> # include <cstring> # define INF 0x3f3f3f using namespace STD; const int maxn = 1010; struct node {int X, Y; int step; int flag;} Q [maxn * maxn]; int R, C; int FX, FY; int step_x [4] = {1,-1, 0, 0}; int step_y [4] = {0, 0, 1,-1 }; char G [maxn] [maxn]; int vis [maxn] [maxn]; int BFS () {int front = 0, rear = 0; memset (VIS, 0, sizeof (VIS); For (INT I = 0; I <r; I ++) {for (Int J = 0; j <C; j ++) {If (G [I] [J] = 'F') {q [rear]. X = J; Q [rear]. y = I; Q [rear]. step = 0; Q [rear ++]. flag = 1 ;}} Q [rear]. X = FX; Q [rear]. y = FY; Q [rear]. step = 0; Q [rear ++]. flag = 0; while (front <rear) {int xx = Q [Front]. x; int YY = Q [Front]. y; If (Q [Front]. flag) {for (INT I = 0; I <4; I ++) {xx = Q [Front]. X + step_x [I]; YY = Q [Front]. Y + step_y [I]; if (XX <0 | XX> = c | YY <0 | YY> = r | G [YY] [XX] = '#' | G [YY] [XX] = 'F ') continue; G [YY] [XX] = 'F'; Q [rear]. X = xx; Q [rear]. y = YY; Q [rear]. flag = 1; Q [rear]. step = Q [Front]. step + 1; ++ rear ;}} else {for (INT I = 0; I <4; I ++) {xx = Q [Front]. X + step_x [I]; YY = Q [Front]. Y + step_y [I]; if (vis [YY] [XX] | G [YY] [XX] = 'F' | G [YY] [XX] = '#') continue; if (XX <0 | XX> = c | YY <0 | YY> = r) return Q [Front]. step + 1; vis [YY] [XX] = 1; Q [rear]. X = xx; Q [rear]. y = YY; Q [rear]. flag = 0; Q [rear ++]. step = Q [Front]. step + 1 ;}}front ++;} return 0 ;}int main () {// freopen ("input.txt", "r", stdin); int T; cin> T; while (t --) {scanf ("% d", & R, & C); FX = FY =-1; getchar (); for (INT I = 0; I <r; I ++) {gets (G [I]); If (FX =-1) {for (Int J = 0; j <C; j ++) {If (G [I] [J] = 'J') {FX = J; FY = I; break ;}}}} int time = BFS (); If (time) printf ("% d \ n", time); else printf ("impossible \ n");} return 0 ;}
-----------