zoj3652 Maze(bfs)

来源:互联网 发布:php编程入门教程 编辑:程序博客网 时间:2024/05/21 22:58
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 rectangle of n*m, and Celica is at (x1,y1) at the beginning while he needs to go to (x2, y2). And Celica has a Mobility ofl. 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 becomel again 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 reduced 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 to (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 follows n lines and each line contains m integers. Thej-th integer p in the line i describe the lattice in thei line and j row. If p euqals to -1, it means you can't get into it. Ifp euqals to 0, it means the lattice is not dominated by any monster. Ifp is larger than 0, it means it is dominated by the p-th monster.

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

Then there follows k lines. The i-th line has two integers mean the position of thei-th monster.

At last, in the n+k+3 lines, there is four integers x1,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 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 (4,3)->(4,4)>(3,4)->(3,5) in turn 3. At last he goes (2,5)->(1,5) in turn 4. 


题意:从起点走到终点要走几回合,体力耗完即开始下一回合,走进怪兽的区域体力立马变0,碰见怪兽立即杀死,然后改怪兽所控制的领域变为可走。

分析:这题着实很多坑点,一不小心进坑了;

1)每一个回合, Celica  的初始行动值为L, 没移动一个位置,行动值-1, 当行动值减为0, 则开始下一个回合
2)一旦踏入怪的所在位置,则立即会将怪杀死,怪所控制的位置也就变为0了
3)当移动到一个怪所控制的位置,行动立即减为0
4)每到达一个位置,首先变化的是行动值, 这就意味着,如果踏入一个怪所在的位置,而且该位置是被怪所控制的情况下,那么行动值先变为0,再将怪杀死
5)怪不一定在它控制的位置
6)起点一定没有怪, 但可能被某一个怪控制

#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 = 1000000007;#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];//三维标记,状态压缩存储怪物被杀情况int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};struct node{    int x,y;    int ans,t,flag;    node(){}//不这样会栈溢出,虽然我也不知道为什么这样,反正我是溢出了    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(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>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))))//判断怪物是否被杀死                    t=0;            }            if(ms[x][y]>=0)//怪物所在位置                flag = now.flag | (1<<(ms[x][y]));            if(vis[x][y][flag]) continue;            int ans=now.ans;            if(t==0)//体力耗尽,开始下一回合            {                t=l;                ans++;            }            //cout<<"--"<<next.x<<" "<<next.y<<" "<<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++)//怪物标记为0~4        {            int a,b;            cin>>a>>b;            ms[a][b]=i;        }        cin>>sx>>sy>>ex>>ey;        if(sx==ex&&sy==ey)//起点和终点重合的情况        {            cout<<0<<endl;            continue;        }        CL(vis, false);        bfs();    }    return 0;}



0 0
原创粉丝点击