Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5040
Http://blog.csdn.net/u011026968/article/details/39586669 with this question
It is similar.
Therefore, it is easy to think of vis [4] [maxn] [maxn], which indicates that the surveillance camera is in four directions.
The three seconds are similar to snakes, but there is still some trouble to handle them. For details, refer to here:
Http://blog.csdn.net/hongrock/article/details/39472269
Well written:
Method:
The dir array is in four clockwise directions.
Den [4] [maxn] [maxn]: whether to delay each time on the graph by 3 seconds
If the current location or next location is in the monitored area, it takes 3 seconds.
However, what hurts a little is that it would take three seconds if it is in the monitored area and the next second is not there. The Discrete Time of this question is different from the actual continuous time.
# Include <cstdio> # include <cstring> # include <queue> using namespace STD; # define Cl (a, B) memset (a, B, sizeof ()) # define in (s) freopen (S, "r", stdin) # define out (s) freopen (S, "W", stdout) const int maxn = 500 + 30; struct node {int X, Y, T; node () {} node (int x, int y, int T): x (x), y (Y ), T (t) {} bool operator <(const node & TM) const {return T> TM. t ;}}; int MX, my, N; char mat [maxn] [maxn]; int vis [4] [maxn] [maxn], den [4] [maxn] [Maxn]; int dir [4] [2] = {-}, {0,-1 }}; inline int phosphatase (char c) {If (C = 'n') return 0; If (C = 's') return 2; if (C = 'W') return 3; if (C = 'E') return 1; Return-1;} inline int legal (int x, int y) {If (x> = 0 & Y> = 0 & x <n & Y <n & mat [x] [Y]! = '#') Return 1; return 0;} int BFS () {priority_queue <node> q; CL (VIS, 0); q. push (node (MX, my, 0); While (! Q. empty () {node TMP = Q. top (); q. pop (); If (vis [TMP. T % 4] [TMP. x] [TMP. y]) continue; If (MAT [TMP. x] [TMP. y] = 'T') return TMP. t; vis [TMP. T % 4] [TMP. x] [TMP. y] = 1; q. push (node (TMP. x, TMP. y, TMP. t + 1); For (INT I = 0; I <4; I ++) {int x = TMP. X + dir [I] [0]; int y = TMP. Y + dir [I] [1]; If (! Legal (x, y) continue; int tmpt = TMP. t; If (den [TMP. T % 4] [x] [Y] | Den [TMP. T % 4] [TMP. x] [TMP. y]) tmpt + = 3; else tmpt ++; If (! Vis [tmpt % 4] [x] [Y]) Q. push (node (X, Y, tmpt);} return-1;} int main () {// In ("hdu5040.txt"); int ncase, Ic = 0; scanf ("% d", & ncase); While (ncase --) {Cl (Den, 0); scanf ("% d", & N ); for (INT I = 0; I <n; I ++) {scanf ("% s", mat [I]); For (Int J = 0; j <N; j ++) {If (MAT [I] [J] = 'M') {MX = I; my = J ;} else {int Al = phosphatase (MAT [I] [J]); If (~ Al) // Al =-1, the condition here is 0 {for (int K = 0; k <4; k ++) {den [k] [I] [J] = 1; // n s e w int x = I + dir [(Al + k) % 4] [0]; int y = J + dir [(Al + k) % 4] [1]; if (x >=0 & Y >=0 & x <n & Y <n) den [k] [x] [Y] = 1 ;}}}} // getchar ();} printf ("case # % d: % d \ n", ++ IC, BFs ();} return 0 ;}
HDU 5040 BFS Multidimensional Processing Diagram