Zoj3652 Maze (bfs)

Source: Internet
Author: User
Tags cmath

Zoj3652 Maze (bfs)

Maze Time Limit: 2 Seconds Memory Limit: 65536 KB

Celica is a brave person and believer of a God in the bright side. he always fights against the monsters that endanger humans. one day, he is asked to go through a maze to do a important task.

The maze is a rectangleN*M, And Celica is (X1,Y1) At the beginning while he needs to go (X2,Y2). And Celica has a MobilityL. When he moves a step the movility will decreased by 1. if his mobility equals to 0, he can't move anymore in this turn. and no matter how much mobility Celica uses in one turn, his movility will becomeLAgain in the next turn. And a step means move from one lattice to another lattice that has an adjacent edge.

However, due to the world's rule and the power of magic, there is something called Dominance in the maze. some lattices may be dominated by some monsters and if Celica goes into these lattices, his mobility will be forced CED to 0 at once by the monster's magic power. and monsters have strong Domain awareness so one lattice won't be dominated by more than one monster.

But luckily, Celica gets a strong power from his God so that he can kill this monsters easily. if Celica goes into a lattice which a monster stands on, he can kill the monster without anytime. if a monsters is killed, the lattices it dominates will no longer be dominated by anyone (or we can say they are dominated by Celica) and these lattices will obey the rule of mobility that normal lattices obey.

As for the task is so important that Celica wants to uses the least turn to go (X2,Y2). Please find out the answer.


PS1: it doesn't matter if Celica doesn't kill all the monsters in the maze because he can do it after the task and a monster may appear at a lattice that is not dominated by it, even a lattice that is not dominated by any monsters.

PS2: We define (1, 1) as the top left corner. And monsters won't move.

PS3: No matter which lattice Celia gets in, the change of mobility happens first.

PS4: We promise that there is no two monsters have same position and no monster will appear at the start point of Celica.

Input


The first contains three integers,N,M,L. (1 ≤N,M≤ 50, 1 ≤L≤ 10)

Then there followsNLines and each line containsMIntegers.J-Th integerPIn the lineIDescribe the lattice inILine andJRow. IfPEuqals to-1, it means you can't get into it. IfPEuqals to 0, it means the lattice is not dominated by any monster. IfPIs larger than 0, it means it is dominated byP-Th monster.

And then inN+ 2 line, there is an integerK(0 ≤K≤ 5) which means the number of monster.

Then there followsKLines.I-Th line has two integers mean the position ofI-Th monster.

At last, inN+K+ 3 lines, there is four integersX1,Y1,X2,Y2.

Output

If Celica can't get to (X2,Y2), Output We need God's help !, Or output the least turn Celica needs.

Sample Input
5 5 42 2 2 1 0-1 2 2 -1 12 2 2 1 11 1 1 1 01 2 2 -1 024 21 15 1 1 55 5 41 1 1 1 11 2 2 -1 -12 2 -1 2 2-1 -1 2 2 22 2 2 2 222 21 21 1 5 5
Sample Output
4We need God's help!
Hit

In the first case, Celica goes to (4, 1) in turn 1. then he goes to (4, 2) in turn 2. after he gets (4, 2), kill the monster 1. he goes through ()-> () in turn 3. at last he goes ()-> () in turn 4.

 

It takes several rounds from the starting point to the ending point. When the physical strength is exhausted, the next round begins. When the physical strength enters the monster area, the physical strength immediately changes to 0. When a monster is killed, then, the Field Controlled by monsters becomes accessible.

Analysis: There are a lot of pitfalls in this question;

 

1) For each round, the initial value of Celica is L. If no position is moved, the value of Celica is-1. When the value of Celica is reduced to 0, the next round is started. 2) once you step into the location of the monster, the monster will be killed immediately, and the location controlled by the monster will be changed to 0. 3) when you move to the location controlled by the monster, the action is immediately reduced to 0 4) when a position is reached, the first change is the action value, which means that if you step into a strange position, in addition, if this position is controlled by a monster, the first action value is 0, and then the monster is killed. 5) the monster is not necessarily in the position it controls. 6) the starting point must not be strange, but it may be controlled by a strange person.
# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      # Include
       
         # Include
        
          # Include
         
           # Include using namespace std; const double eps = 1e-6; const double pi = acos (-1.0); const int INF = 0x3f3f3f; const int MOD = 1000000007; # define ll long # define CL (a, B) memset (a, B, sizeof (a) int n, m, l, k; int sx, sy, ex, ey; int mat [55] [55], MS [55] [55]; bool vis [55] [55] [32]; // 3D tag, status compression stores the killing status of monsters. int dir [4] [2] = {0, 1}, {0,-1 }}; struct node {int x, y; int ans, t, flag; node () {}// if this is not the case, stack overflow occurs. Although I do not know why Positive I overflows node (int x, int y, int ans, int t, int flag): x (x), y (y), ans (ans ), t (t), flag (flag) {} bool friend operator <(const node & a, const node & B) {if (. ans! = B. ans) return a. ans> B. ans; return a. t <B. t ;}}; void bfs () {priority_queue
          
            Q; node now; vis [sx] [sy] [0] = true; q. push (node (sx, sy, 0, l, 0); while (! Q. empty () {now = q. top (); q. pop (); if (now. x = ex & now. y = ey) {if (now. t! = L) now. ans ++; cout <
           
             N | y <= 0 | y> m) continue; if (mat [x] [y] =-1) continue; int t = now. t-1; int flag = now. flag; if (mat [x] [y]! = 0) {int re = mat [x] [y]; if (! (Now. flag & (1 <(re-1) // determine whether the monster is killed t = 0;} if (MS [x] [y]> = 0) // The location where the monster is located. flag = now. flag | (1 <(MS [x] [y]); if (vis [x] [y] [flag]) continue; int ans = now. ans; if (t = 0) // physical exhaustion, starting the next round {t = l; ans ++;} // cout <-- <
            
              > N> m> l) {for (int I = 1; I <= n; I ++) for (int j = 1; j <= m; j ++) cin> mat [I] [j]; cin> k; CL (MS,-1); for (int I = 0; I
             
               > A> B; MS [a] [B] = I;} cin> sx> sy> ex> ey; if (sx = ex & sy = ey) // the combination of the start point and the end point {cout <0 <
              
               

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.