Reprinted please indicate the source, thank you http://blog.csdn.net/acm_cxlove/article/details/7854526
By --- cxlove
Question: Give a maze where K monsters are in some locations, and each monster controls some locations to cause damage to the hero.
Initially there is a moving force. Every step is taken, the moving force is-1. If you enter the position controlled by the monster and the monster is not eliminated, the moving force is reduced to 0 and enters the next round, if the monsters are eliminated, all the areas controlled by the monsters are considered as open spaces. Ask how many rounds are required to reach the target location.
Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 4842
It is actually a very boring BFS question.
The question is also boring.
Note the following points: If the start point and the end point overlap, the answer is 0, and the first round is not started.
If it reaches a certain position, it is the end point, but the movement is 0, it does not matter, so it is to determine whether the end is complete, and then enter the next round
Even if a location is open space, there can also be monsters. The region controlled by monster 1 can also be monster 2. It was a bit dizzy when it was done...
4-dimensional determination, X coordinate, Y coordinate, current moving force value, and elimination of monsters (State compression)
# Include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <set> # include <string> # include <queue> # define INF 1 <28 # define M 6000005 # define n 205 # define maxn 300005 # define EPS 1e-8 # define zero () FABS (a) <EPS # define min (A, B) (a) <(B )? (A) :( B) # define max (A, B) (a)> (B )? (A) :( B) # define Pb (a) push_back (a) # define MEM (a, B) memset (a, B, sizeof ()) # define ll long # define lson step <1 # define rson step <1 | 1 using namespace STD; int N, M, L, maze [55] [55]; struct node {int X, Y, HP, step, State; node () {} bool check () {If (x> 0 & x <= N & Y> 0 & Y <= m) return true; return false ;} // priority queue bool operator <(const node N1) const {return n1.step! = Step? Step> n1.step: HP <n1.hp ;}}s, E, pre, cur; int way [4] [2] = {, 0,-, 0, -1, 0}, K; bool flag [55] [55] [11] [1 <5]; int mon [55] [55]; void BFS () {// This step directly determines if (S. X = E. X & S. y = E. y) {printf ("0 \ n"); return;} MEM (flag, false); S. HP = L; S. step = 1; S. state = 0; priority_queue <node> que; while (! Que. empty () que. pop (); flag [S. x] [S. y] [l] [0] = true; que. push (s); While (! Que. empty () {pre = que. top (); que. pop (); // note that when HP is 0, the end can also be reached, and the end can be reached. // it is determined that the end is reached first, and then enters the next round of IF (pre. X = E. X & pre. y = E. y) {printf ("% d \ n", pre. step); return;} // if the value is 0, the next round of IF (pre. HP = 0) {pre. step ++; pre. HP = L; If (! Flag [pre. x] [pre. y] [pre. HP] [pre. state]) {flag [pre. x] [pre. y] [pre. HP] [pre. state] = true; que. push (pre) ;}continue ;}for (INT I = 0; I <4; I ++) {cur = pre; cur. X + = way [I] [0]; cur. Y + = way [I] [1]; If (! Cur. check () | maze [cur. x] [cur. y] <0) continue; If (maze [cur. x] [cur. y] = 0 | (1 <(maze [cur. x] [cur. y]-1) & cur. state) {// If (Mon [cur. x] [cur. y]> = 0) cur. state | = (1 <mon [cur. x] [cur. y]); cur. HP --;} else {If (Mon [cur. x] [cur. y]> = 0) cur. state | = (1 <mon [cur. x] [cur. y]); cur. HP = 0;} If (! Flag [cur. x] [cur. y] [cur. HP] [cur. state]) {flag [cur. x] [cur. y] [cur. HP] [cur. state] = true; que. push (cur) ;}} puts ("we need God's help! ");} Int main () {While (scanf (" % d ", & N, & M, & L )! = EOF) {for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= m; j ++) {scanf ("% d", & maze [I] [J]) ;}} scanf ("% d", & K); MEM (Mon,-1 ); for (INT I = 0; I <K; I ++) {int X, Y; scanf ("% d", & X, & Y ); mon [x] [Y] = I;} scanf ("% d", & S. x, & S. y, & E. x, & E. y); BFS ();} return 0 ;}