HDU-5040-Instrusive (BFS + priority queue), instrusive
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 ");}}
Width-first search (BFS), you can give a code framework, by the way, to describe ideas ,,
Breadth-first search (BFS): a simple and easy-to-use search method, usually used in combination with queues or priority queues. Algorithm idea: Starting from one point, the vertex to be searched is put into the queue, the search vertex is popped up, and then the vertex to be searched is put into the queue, so as to achieve priority search in the breadth direction. For the width-first search method, the structure of the State node and the node expansion rule are different for different problems, but the search policy is the same, so the algorithm framework is basically the same. Struct tnode {// define the data type of a node ...... // Determine the required data type based on the specific problem} state [maxn]; // define an array of the tnode type as the queue void init () of the storage node (); // initialize the function bool extend (); // determine whether the node can be extended. If yes, a new node bool repeat () is generated (); // check whether bool find () has been found on the new node in the queue. // check whether the new node is the target node void outs (); // output the node status void printpath (); // output path // jsonint main () {init (); bfs () ;}// jsonvoid init () {// initialization} // jsonvoid bfs () // BFS algorithm main program {tnode temp; // tnode temporary node int head = 0, tail = 0; // queue head pointer and tail pointer while (head <= tail & tail <maxn) {// The queue is not empty and the data space is used up in a cycle // determine a node Extension Rule temp = state [head] based on the specific problem; // get the node of the queue header if (extend ())...... remaining full text>