Question link:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4528
Question meaning:
To give you a matrix, '.' indicates that it can pass, and 'x' indicates that it cannot pass, telling you the position of S, D, and E.
Ask if you can find D and E at the same time within the specified time starting from S. As long as there is no obstacle between two people, you can find it.
Solution:
BFS: When recording accessed nodes, pay attention to the status confirmation.
Vis [x] [y] [I] [J] indicates whether the access coordinates are X, Y, and the states of D and E are displayed. I = 0 indicates that D is not seen, I = 1 indicates that D is seen, j = 0 indicates that E is not seen, j = 1 indicates that E is seen.
Paste a set of test data:
7 7 19
S ......
. X .....
.. X ....
... X...
.... X ..
..... XD
...... E
Answer: 18
Code:
# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <stack> # include <list> # include <queue> # define EPS 1e-6 # define Inf (1 <30) # define PI ACOs (-1.0) using namespace STD; char save [120] [120]; bool se [2] [120] [120], vis [120] [120] [2] [2]; struct Po {int X, Y, T; bool flag1, flag2;} s, D, E; int dir [4] [2] = {0,-, 1 ,-1, 0}; int n, m, T; bool check (int x, int y) {If (x <1 | x> N | Y <1 | Y> m) return false; return true;} void Init (int x, int y, int num) {for (INT I = 0; I <4; I ++) {int xx = x, YY = y; while (true) // pre-processing can find the locations D and E {xx + = dir [I] [0], YY + = dir [I] [1]; If (check (XX, YY) = false | save [XX] [YY] = 'X') break; if (num = 0 & save [XX] [YY] = 'E') // pay attention to the position of start S, and treat it. break; If (num = 1 & save [XX] [YY] = 'D') break; se [num] [XX] [YY] = true ;}} return;} Bo Ol flag; int BFS () {queue <struct Po> myq; S. T = 0; myq. Push (s); While (! Myq. empty () {struct Po cur = myq. front (); myq. pop (); For (INT I = 0; I <4; I ++) {int xx = cur. X + dir [I] [0], YY = cur. Y + dir [I] [1], TT = cur. t + 1; if (tt> T) break; If (check (XX, YY) = false | save [XX] [YY]! = '. ') Continue; struct Po temp; If (SE [0] [XX] [YY] | cur. flag1) // If D is found, or D temp is found. flag1 = true; else temp. flag1 = false; If (SE [1] [XX] [YY] | cur. flag2) temp. flag2 = true; else temp. flag2 = false; If (temp. flag1 & temp. flag2) {flag = true; return tt;} If (vis [XX] [YY] [temp. flag1] [temp. flag2]) // continue; temp has been found before the status. X = XX, temp. y = YY, temp. T = tt; vis [XX] [YY] [temp. flag1] [temp. flag2] = true; myq. push (temp) ;}} return-1 ;}int main () {int tt; scanf ("% d", & TT); For (int ca = 1; ca <= tt; CA ++) {scanf ("% d", & N, & M, & T); For (INT I = 1; I <= N; I ++) {scanf ("% s", save [I] + 1); For (Int J = 1; j <= m; j ++) {If (save [I] [J] = 's') s. X = I, S. y = J; If (save [I] [J] = 'D') d. X = I, D. y = J; If (save [I] [J] = 'E') E. X = I, e. y = J ;}// printf ("s: % d E: % d \ n", S. x, S. y, E. x, E. y, D. x, D. y); memset (SE, false, sizeof (SE); Init (D. x, D. y, 0); // coordinate Init (E. x, E. y, 1); // The coordinate flag of e that can be seen by preprocessing is false; If (SE [0] [S. x] [S. y]) s. flag1 = true; else S. flag1 = false; If (SE [1] [S. x] [S. y]) s. flag2 = true; else S. flag2 = false; printf ("case % d: \ n", CA); If (S. flag1 & S. flag2) {printf ("0 \ n"); continue;} memset (VIS, false, sizeof (VIS); vis [S. x] [S. y] [S. flag1] [S. flag2] = true; int ans = BFS (); If (flag = false) printf ("-1 \ n"); else printf ("% d \ n ", ans);} return 0 ;}