Problem descriptionthe legendary mercenary solid Matt gets a classic mission: Infiltrate a military base.
The military base can be seen as an N * n grid. Matt's target is in one of the grids and Matt is now in another grid.
In normal case, Matt can move from a grid to one of the four neighbor grids in a second. But this mission is not easy.
Around the military base there are fences, Matt can't get out of the base.
There are some grids filled with obstacles and Matt can't move into these grids.
There are also some surveillance cameras in the grids. every camera is facing one of the four direction at first, but for every second, they will rotate 90 degree clockely Wis. every camera's sight range is 2, which means that if Matt is in the same grid as the camera, or in the grid that the camera is facing, he will be seen immediately and the mission will fail.
Matt has a special equipment to sneak: A cardbox. matt can hide himself in the card box and move without being noticed. but in this situation, Matt will have to use 3 seconds to move 1 grid. matt can also just hide in the cardbox without moving. the time to hide and the time to get out of the cardbox can be ignored.
Matt can't take the risk of being noticed, so he can't move without cardbox into a grid which is now insight of cameras or from a grid which is now insight of cameras. what's more, Matt may be in the cardbox at the beginning.
As a live legend, Matt wants to complete the mission in the shortest time.
Inputthe first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.
For each test cases, the first line contains one INTEGER: n (1 <= n <= 500)
In the following n lines, each line contains n characters, indicating the grids.
There will be the following characters:
● '.' For empty
● '#' For Obstacle
● 'N' for camera facing north
● 'W' for camera facing west
●'S for camera facing south
● 'E' for camera facing east
● 'T' for target
● 'M' for Matt
Outputfor each test case, output one line "case # X: Y", where X is the case number (starting from 1) and Y is the answer.
If Matt cannot complete the mission, output '-1 '.
Sample Input
23M...N...T3M.. ###..T
Sample output
Case #1: 5Case #2: -1
Source2014 ACM/ICPC Asia Regional Beijing online
Idea: from one point to another, no point can be taken directly. So for any target point, if there is a light, you can directly hide in the box, if it is open space, it can be up to three seconds. For details, see the code.
# Include <cstdio> # include <queue> # define INF 99999999; using namespace STD; char MP [500] [505]; int N, ex, ey, NXT [4] [2] = {500}, {500}, {0,-1}, {-}; int vis [] []; struct s {int X, Y, step; bool operator <(const S & P) const {return Step> P. step ;}} t; bool check (int x, int y, int time) // determines whether the point is directed to {If (MP [x] [Y]! = '.') Return 0; If (x-1> = 0 & MP [x-1] [Y]! = '. ') {Switch (time) {Case 0: If (MP [x-1] [Y] = 's') return 0; break; Case 1: if (MP [x-1] [Y] = 'E') return 0; break; Case 2: If (MP [x-1] [Y] = 'n ') return 0; break; Case 3: If (MP [x-1] [Y] = 'W') return 0; break ;}} if (x + 1 <n & MP [x + 1] [Y]! = '. ') {Switch (time) {Case 0: If (MP [x + 1] [Y] = 'n') return 0; break; Case 1: if (MP [x + 1] [Y] = 'W') return 0; break; Case 2: if (MP [x + 1] [Y] = 's') return 0; break; Case 3: if (MP [x + 1] [Y] = 'E') return 0; break;} If (Y-1> = 0 & MP [x] [Y-1]! = '. ') {Switch (time) {Case 0: If (MP [x] [Y-1] = 'E') return 0; break; Case 1: if (MP [x] [Y-1] = 'n') return 0; break; Case 2: If (MP [x] [Y-1] = 'W ') return 0; break; Case 3: If (MP [x] [Y-1] = 's') return 0; break ;}} if (Y + 1 <n & MP [x] [Y + 1]! = '. ') {Switch (time) {Case 0: If (MP [x] [Y + 1] = 'W') return 0; break; Case 1: if (MP [x] [Y + 1] = 's') return 0; break; Case 2: if (MP [x] [Y + 1] = 'E') return 0; break; Case 3: if (MP [x] [Y + 1] = 'n') return 0; break;} return 1 ;}int main () {int T, I, J, time, cases = 1; scanf ("% d", & T); While (t --) {scanf ("% d", & N); for (I = 0; I <n; I ++) scanf ("% s", MP [I]); printf ("case # % d:", cases ++ ); for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) {If (MP [I] [J] = 'M') MP [I] [J] = '. ', T. X = I, T. y = J; else if (MP [I] [J] = 'T') MP [I] [J] = '. ', Ex = I, ey = J ;}} for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) vis [I] [J] = inf; T. step = 0; vis [T. x] [T. y] = 0; priority_queue <S> que; que. push (t); While (! Que. empty () {T = que. top (); If (T. X = ex & T. y = ey) {printf ("% d \ n", T. step); break;} que. pop (); T. step ++; for (I = 0; I <4; I ++) {T. X + = NXT [I] [0]; T. Y + = NXT [I] [1]; If (MP [T. x] [T. y] = '#' | T. x <0 | T. x> = n | T. Y <0 | T. y> = N) {T. x-= NXT [I] [0]; T. y-= NXT [I] [1]; continue;} If (MP [T. x] [T. y] = '. ') // if the target point is an empty space {for (j = 0; j <3; j ++) // a maximum of three seconds {time = (T. step + J-1) % 4; If (check (T. x, t. y, time) & check (T. x-NXT [I] [0], T. y-NXT [I] [1], time) & T. step + j <vis [T. x] [T. y]) // neither the start point nor the target point is taken to {T. step + = J; vis [T. x] [T. y] = T. step; que. push (t); T. step-= J; break; // you can find the minimum time to arrive.} If (j = 3) // if you haven't waited for three seconds, go to {T. step + = 2; If (T. step <vis [T. x] [T. y]) {vis [T. x] [T. y] = T. step; que. push (t);} t. step-= 2 ;}} else // if the target point is a lamp, go to {T. step + = 2; If (T. step <vis [T. x] [T. y]) {vis [T. x] [T. y] = T. step; que. push (t);} t. step-= 2;} t. x-= NXT [I] [0]; T. y-= NXT [I] [1] ;}} if (que. empty () printf ("-1 \ n ");}}
HDU-5040-Instrusive (BFS + priority queue)