Test instructions The minimum time required from the position of a to the position of R in the maze to go through '. ' squares need 1s through ' x ' squares need two seconds ' # ' to indicate wall
Because of the 1s and 2s two cases need to add some judgment on the base maze BFS
The time at which each point is reached is initially infinite when it takes more time to reach that point from one point to the point of time than his original time, and the point is scanned for a complete picture to get an answer.
#include <cstdio> #include <cstring> #include <queue> using namespace std;
const int N = 205;
Char Mat[n][n];
int Time[n][n], SX, SY;
int dx[4] = {0, 0,-1, 1};
int dy[4] = {-1, 1, 0, 0};
struct Grid {int x, y;
Grid (int xx = 0, int yy = 0): X (xx), Y (yy) {}};
void BFs () {memset (time, 0x3f, sizeof (time));
Time[sx][sy] = 0;
Queue<grid> G;
G.push (Grid (SX, SY));
while (!g.empty ()) {Grid cur = G.front ();
G.pop ();
int cx = cur.x, cy = cur.y, CT = time[cx][cy];
for (int i = 0; i < 4; ++i) {int NX = CX + dx[i], NY = cy + dy[i];
if (Mat[nx][ny] && mat[nx][ny]! = ' # ') {int tt = CT + 1;
if (mat[cx][cy] = = ' x ') ++tt;
if (TT < Time[nx][ny]) {Time[nx][ny] = TT;
G.push (Grid (NX, NY)); }}}}} int main () {inT N, M, ex, EY;
while (~SCANF ("%d%d", &n, &m)) {memset (mat, 0, sizeof (MAT));
for (int i = 1; I <= n; ++i) scanf ("%s", Mat[i] + 1); for (int i = 1; l <= N; ++i) for (int j = 1; j <= m; ++j) if (mat[i][j] = = ' a ') SX = i, sy
= J;
else if (mat[i][j] = = ' r ') ex = i, ey = j;
BFS ();
if (Time[ex][ey] = time[0][0]) printf ("%d\n", Time[ex][ey]);
else printf ("Poor ANGEL have to stay in the prison all his life.\n");
} return 0; }