This is a basic BFS question. Different from other questions, this question needs to initialize the time when each grid fires.
I participated in the competition these days and found that,AlgorithmIt is really wrong to use an endless template. You must use it flexibly and use your smart brain to apply better algorithms!
In addition, the ability to analyze problems is also very important. A question may be very long, but the most useful thing is only a few words. From those words, we can extract some substantive things, that's the idea of solving the problem!
This question is actually a two-step Request for the shortest path, the first request for the shortest path, and the second request for the shortest path! Another idea is the Super source. This is a bit more than a piece of thinking. In the process of solving problems, the network will be used more.
Here we put the start time, all fire points in the queue, and the shortest path is set to 0, which is equivalent to regard them as the start point.
DetailsCodeAs follows:
# Include <cstdio> # include <vector> # include <iostream> # include <queue> # include <algorithm> using namespace STD; const int n = 1010; const int INF = 100000; int t, n, m, ans; int Tim [N] [N], d [N] [N]; int A [4] = {0, 0, 1,-1}, B [4] = {1,-1, 0, 0 }; char G [N] [N]; queue <pair <int, int> q; pair <int, int> S, U, TMP; void inittime () {While (! Q. empty () {u = Q. front (); q. pop (); int UX = u. first, Uy = u. second; For (INT I = 0; I <4; ++ I) {int x = UX + A [I], y = Uy + B [I]; if (Tim [x] [Y]> Tim [UX] [Uy] + 1 & G [x] [Y]! = '#') {Tim [x] [Y] = Tim [UX] [Uy] + 1; TMP. first = x, TMP. second = y; q. push (TMP) ;}}} void BFS () {d [S. first] [S. second] = 0; queue <pair <int, int> q; q. push (s); ans = inf; while (! Q. empty () {u = Q. front (); q. pop (); int UX = u. first, Uy = u. second; // cout <UX <<''' <Uy <Endl; if (UX = 1 | UX = n | Uy = 1 | Uy = m) {ans = d [UX] [Uy]; break ;} for (INT I = 0; I <4; ++ I) {int x = UX + A [I], y = Uy + B [I]; if (G [x] [Y] = '. '& D [x] [Y]> d [UX] [Uy] + 1 & D [UX] [Uy] + 1 <Tim [x] [Y]) {d [x] [Y] = d [UX] [Uy] + 1; // cout <D [x] [Y] <''<time [x] [Y] <" zuobi AO "<x <'' <Y <Endl; TMP. first = x, TMP. second = y; q. push (TMP) ;}}} int main () {scanf ("% d", & T); While (t --) {While (! Q. empty () Q. pop (); scanf ("% d", & N, & M); getchar (); For (INT I = 0; I <= n + 1; ++ I) g [I] [0] = G [I] [M + 1] = '#'; For (Int J = 0; j <= m + 1; ++ J) g [0] [J] = G [n + 1] [J] = '#'; For (INT I = 0; I <= N; ++ I) for (Int J = 0; j <= m; ++ J) Tim [I] [J] = d [I] [J] = inf; for (INT I = 1; I <= N; ++ I, getchar () for (Int J = 1; j <= m; ++ J) {scanf ("% C", & G [I] [J]); If (G [I] [J] = 'F') {TMP. first = I, TMP. second = J; Tim [I] [J] = 0; q. push (TMP);} else if (G [I] [J] = 'J') s. first = I, S. second = J ;}inittime (); // For (INT I = 1; I <= N; ++ I, cout <Endl) for (Int J = 1; j <= m; ++ J) cout <time [I] [J] <"; BFS (); // For (INT I = 1; I <= N; ++ I, cout <Endl) for (Int J = 1; j <= m; ++ J) cout <D [I] [J] <""; if (ANS = inf) printf ("impossible \ n "); else printf ("% d \ n", ANS + 1 );}}