2014 Beijing Network Competition 09, HDU 5040
This online competition is really miserable, and it's strange that I didn't think about the problem strategy. After I cut the water questions such as the sign-in question, I went to see the 06 frog question. As a result, t_t was thrown into the trap by the dead frog... After four hours, I didn't figure it out... I kneel down to the frog... At that time, I was prepared to answer this question, and the question description was also very painful. Some people said that it would be better to go directly to clarification without reading the question. This also shows that the description of this question is indeed not clear, although not so exaggerated, I still have to read the questions.
It is not very difficult to review this question. If it is the shortest time, then of course we think of BFS. If it is pure BFs, it is only the shortest step. Therefore, we need to add a priority queue. State, only the coordinates (x, y) and the time cur_t % 4 are required.
Explain why it is cur_t % 4. Think carefully and you will find that the differences between each status in this question, except for the coordinates of the current position, are also the turning of all monitors, this turn is a 4-Cycle cycle. So, if the coordinates of one or two States are the same and T % 4 is the same, this is a state, you do not need to press the queue for the second time once. In addition to this, there should be no other difficulties. Later, I wrote a piece of code, and it was a hit .. T_t why didn't I do this first?
Code:
# Include <iostream> # include <cstdio> # include <queue> # include <cstring> # include <algorithm> # define n 511 using namespace STD; int N, dir [4] [2] = {0,-1}, {-}, {}, {}; // E = 0, S = 1, W = 2, n = 3int change [110]; bool vis [N] [N] [5]; char a [n] [N]; struct node {int X, Y, T; node () {;}// constructor node (int xx, int YY, int TT) {x = XX, y = YY, T = tt;} // constructor bool operator <(const node s) const {return T> S. t ;}}; int check (int x, int y, int T) // return 1 indicates that the current position is illuminated by a light. 2 indicates that no light is shining at the current position. You can also go, 0 indicates that the value is not up to {If (x <1 | x> N | Y <1 | Y> N | A [x] [Y] = '#') return 0; If (change [A [x] [Y]! =-1) return 1; for (INT I = 0; I <4; I ++) // you need to know if the current vertex has been taken, you need to check whether the light is directed to the current point in four directions: {int xx = x + dir [I] [0], YY = Y + dir [I] [1]; if (XX <1 | XX> N | YY <1 | YY> N) continue; int TMP = change [A [XX] [YY]; // TMP> = 0 indicates that the current vertex has a lamp if (TMP> = 0 & (TMP + T) % 4 = I) return 1; // (TMP + T) % 4 indicates the current time. % 4 indicates the turn of the lamp at this time. It slightly designs the direction function dir so that when (TMP + T) = I, indicates that the vertex is taken to} return 2;} int BFS (INT Sx, int Sy) {memset (VIS, 0, sizeof (VIS); priority_queue <node> que; que. push (node (sx, Sy, 0 )); While (! Que. empty () {node TMP = que. top (); que. pop (); if (a [TMP. x] [TMP. y] = 'T') return TMP. t; int cur_t; // cur_t = TMP. t + 1; if (! Vis [TMP. x] [TMP. y] [cur_t % 4]) vis [TMP. x] [TMP. y] [cur_t % 4] = 1, que. push (node (TMP. x, TMP. y, cur_t); int flag = check (TMP. x, TMP. y, TMP. t); // flag = 1 indicates that the current vertex is illuminated for (INT I = 0; I <4; I ++) {int Cx = TMP. X + dir [I] [0], Cy = TMP. Y + dir [I] [1]; int che = check (CX, Cy, TMP. t); If (CHE = 0) continue; If (CHE = 1 | flag = 1) cur_t = TMP. T + 3; // The vertex to be moved or the current vertex is illuminated. Else if (CHE = 2) cur_t = TMP. t + 1; if (! Vis [CX] [Cy] [cur_t % 4]) vis [CX] [Cy] [cur_t % 4] = 1, que. push (node (CX, Cy, cur_t) ;}} return-1 ;}int main () {# ifndef online_judge freopen ("D:/in.txt", "r ", stdin); # endif // online_judge memset (change,-1, sizeof (Change); change ['E'] = 0, change ['s'] = 1, change ['W'] = 2, change ['n'] = 3; int T, CAS = 1; scanf ("% d", & T ); while (t --) {scanf ("% d", & N); For (INT I = 1; I <= N; I ++) scanf ("% s ", A [I] + 1); int Sx, Sy; For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) if (a [I] [J] = 'M') SX = I, Sy = J; int ans = BFS (sx, sy); printf ("case # % d: % d \ n", CAS ++, ANS);} return 0 ;}
HDU 5040 instrusive [BFS + priority queue]