ZOJ 3652 Maze

Source: Internet
Author: User

ZOJ 3652 Maze

 

MazeTime Limit: 4000/2000 ms (Java/Other) Memory Limit: 131072/65536 K (Java/Other) Total Submission (s): 44 Accepted Submission (s ): 13 Problem Description 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.


 

Status Compression

There is a map, which is divided into several monsters. Before killing a corresponding monster, you can only move one of them on the monster's territory every round, and then enter the next round.

Each round of pig has a powerful power. It takes at least a few times for pig to fit into the destination.

 

Because up to five monsters exist, status compression can be used to save the situation where each vertex in the queue kills the monsters.

Four-Dimensional array marking can be used to identify duplicates

 

There are several pitfalls

1. Start Point and end point are the same for 1 round

2. Do not wait for the next round when the final point is reached.

3. Monsters are not necessarily on their own websites

 

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        # Include
         
           # Define inf 1 <30 # define LL long # define maxn 1 <24 using namespace std; int Map [55] [55]; // map int Mp [55] [55]; // point where the monster is located... A little lazy bool vis [55] [55] [1 <6] [11]; // mark int dir [4] [2] = {0, 1, 0, -, 0,-}; int n, m, l; int k; struct node {int x, y; int t; // moving force int T; // return number int vis; // state compression friend bool operator <(node a, node B) {return. t> B. t;} bool cheak () {if (x> 0 & x <= n & y> 0 & y <= m) return true; return false ;}} st, e, ed; void bfs () {st. t = l; st. T = 1; st. vis = 0; priority_queue
          
            Q; q. push (st); vis [st. x] [st. y] [st. vis] [st. t] = true; while (q. size () {st = q. top (); q. pop (); // cout <
           
            

 

 

 

Related Article

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.