zoj3652 Maze (BFS)

Source: Internet
Author: User

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's asked to go through a maze to do a important task.

The maze is a rectangle n of * m , and Celica are at ( x1 ,) at the beginning while y1 he needs to go ( x2 , ). And Celica has a Mobility of l . When he moves a step the movility would decreased by 1. If He mobility equals to 0, he can ' t move anymore in this turn. And no matter how much mobility Celica uses on one turn, his movility would become l again in the next turn. And a step means move from the one lattice to another lattice that have 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 is dominated by Some monsters and if Celica goes into these lattices, his mobility'll be reduced to 0 At once by the monster ' s magic power. and monsters has strong "Domain awareness" so one lattice won ' t is dominated by more than one monster.

But luckily, Celica gets a strong power from the He 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 would no longer be dominated by anyone (or we can say they are dominated by Celica) and these lattices would obey the rule of mobility that normal lattices obey.

As for the task was so important this Celica wants to uses the least turn to go to ( x2 , y2 ). Please find the answer.


Ps1:it doesn ' t matter if Celica doesn ' t kill all the monsters in the maze because he can does It after the task and a Monst Er may appear at a lattice, which is not dominated by it, even a lattice so is not dominated by any monsters.

Ps2:we define 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 monsters has same position and no monster would 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. Thej-th integerpIn the lineiDescribe the lattice in theiLine andjRow. IfpEuqals to-1, it means you can ' t get into it. IfpEuqals to 0, it means the lattice are not dominated by any monster. Ifpis larger than 0, it means it's dominated by thep-th Monster.

And then in then+2 line, there are an integerk(0≤k≤5) which means the number of monster.

Then there followskLines. Thei-th line have integers mean the position of thei-th Monster.

At last, then+k+3 lines, there is four integersx1,y1,x2,y2.

Output

If Celica can ' t get to the ( 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-2 22 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 (4,3) > (3,4), (3,5) in turn 3. At last he goes (2,5), (1,5) in turn 4.


Test instructions: From the beginning to the end of a few rounds to go, physical exhaustion is the beginning of the next round, into the monster's area of physical strength immediately changed to 0, the monster immediately killed, and then change the field control of the beast to go.

Analysis: This problem really a lot of pits, accidentally into the pit;

1) every turn,Celica  The initial action value is L, without moving a position, the action value-1, when the action value is reduced to 0, then start the next round 2) once stepping into the location of the strange, will immediately kill the blame, the location of the control will become 0 3) when moving to a strange control position, the action immediately reduced to 04) each to a position, The first change is the action value, which means that if you step into a strange position, and the position is under the control of the situation, then the action value first to 0, and then kill 5 The blame is not necessarily in its control position 6) starting point must not be strange, but may be a strange control
#include <iostream> #include <cstdio> #include <cstring> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <cmath> #include <algorithm> Using namespace Std;const double eps = 1e-6;const double pi = ACOs ( -1.0); const int INF = 0x3f3f3f3f;const int MOD = 100000 0007; #define LL Long 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];//Three-dimensional mark, state compression store monster killed case int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};struct node{int x, y;    int Ans,t,flag; Node () {}//does not stack overflow, although I do not know why so, anyway I was overflow node (int x, int y, int ans, int t, int flag): x (x), Y (y), ans (ans), t (t), Flag (fl  AG) {} bool friend operator < (const node &a, const node &b) {if (A.ans! = B.ans) return A.ans >        B.ans;    return A.T < b.t;    }};void BFs () {priority_queue<node> 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<<now.ans<<endl;        return;            } for (int i=0; i<4; i++) {int x=now.x+dir[i][0];            int y=now.y+dir[i][1]; if (x<=0|x>n| | y<=0| |            Y&GT;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)))//Judging whether monsters are killed t=0; } if (ms[x][y]>=0)//Monster Location flag = Now.flag |            (1<< (Ms[x][y]));            if (Vis[x][y][flag]) continue;            int Ans=now.ans;                if (t==0)//physical exhaustion, start the next round {t=l;            ans++; }//cout<< "--" <<next.x<< "" <<next.y<< "" &Lt;<next.ans<<endl;            Vis[x][y][flag]=true;        Q.push (node (x, y, ans, t, flag)); }} cout<< "We need God ' s help!"    <<endl; return;} int main () {while (cin>>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<k; i++)//monster marked as 0~4 {int A, B;            cin>>a>>b;        Ms[a][b]=i;        } cin>>sx>>sy>>ex>>ey;            if (Sx==ex&&sy==ey)//Start and end coincident condition {cout<<0<<endl;        Continue        } CL (Vis, false);    BFS (); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

zoj3652 Maze (BFS)

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.