There are some ignition points in the labyrinth of the test instructions N*m, and the next second of the adjacent non-wall points of each ignition point will be a fire point. Joe can move one step at a time to an adjacent point to give you the location of all the ignition points and Joe's position. Ask Joe the minimum amount
You can save the first ignition time of each point by the BFS first. Only Joe arrives at that point for less than this time Joe can walk this point only need to start with the point where Joe is located. It's important to note that there may be multiple ignition points at the beginning. I started to think that there was only one ignition point.
V[I][J] First time BFS record point i,j the earliest ignition times second time BFS Records Joe's arrival at that point.
#include <bits/stdc++.h>using namespace Std;const int N = 1005;int x[] = {0, 0,-1, 1};int y[] = {-1, 1, 0, 0};int V[n][n], N, M, ans, le, ri;pair<int, int> q[n * N];char g[n][n];void BFS () {int CR, CC, R, C; while (Le < RI) {cr = Q[le].first, CC = Q[le++].second; for (int i = 0; i < 4; ++i) {r = cr + X[i], c = cc + y[i]; if (R < 0 | | r >= N | | C < 0 | | c >= m) ans = ans? ANS:V[CR][CC]; else if (g[r][c] = = '. ' && (!v[r][c] | | v[cr][cc] + 1 < v[r][c]) v[r][c] = v[cr][cc] + 1, q[ri++ ] = Make_pair (R, c); }}}int Main () {int CAs, JR, JC; scanf ("%d", &cas); while (cas--) {scanf ("%d%d", &n, &m); for (int i = 0; i < n; ++i) scanf ("%s", G[i]); memset (V, 0, sizeof (v)); Le = ri = 0; for (int i = 0, i < n; ++i) for (int j = 0; j < m; ++j) if (g[i][j] = = ' F ') q[ri++] = Make_pair (i, J), v[i][j] = 1; else if (g[i][j] = = ' J ') Jr = i, JC = j; BFS (); Le = ri = ans = 0, V[JR][JC] = 1; q[ri++] = Make_pair (Jr, JC); BFS (); if (ans) printf ("%d\n", ans); Else puts ("impossible"); } return 0;}
Topic linkshttp://uva.onlinejudge.org/external/116/11624.pdf
UVa 11624 fire! (BFS fleeing Fire)