zoj 3652 ZOJ 3652 MAZE(BFS+状态压缩)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4842

ZOJ Problem Set - 3652
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 4
2 2 2 1 0
-1 2 2 -1 1
2 2 2 1 1
1 1 1 1 0
1 2 2 -1 0
2
4 2
1 1
5 1 1 5
5 5 4
1 1 1 1 1
1 2 2 -1 -1
2 2 -1 2 2
-1 -1 2 2 2
2 2 2 2 2
2
2 2
1 2
1 1 5 5

Sample Output

4
We 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.

Author: ZHANG, Ruijie

题目大意:打怪兽。给一个n*m的地图,其中1~5表示该位置被第i个怪兽管理着,0表示空地可以走,-1表示这个位置不可以走。

再输入一个k,接下去k行怪兽所在的坐标。

最后一行就是Celica的起点和终点,问最少需要多少回合才能走到终点。

特别注意:

1、每一个回合开始,Celica的初始体力值为L,每移动一次,体力值减1,当体力值==0时,开始下一回合。

2、只要进入怪兽所管理的位置,体力值就瞬间变为0。

3、如果进入到怪兽所在的位置,就要将怪兽打死,体力值变为0,。与此同时,地图上怪兽的位置和怪兽所控制的位置全部变为0(无障碍空地)。

4、起点没有怪,但可能被某一个怪兽控制。

5、如果起点和终点重合,直接输出0。即回合数为0.

6、怪兽不一定在他控制的位置,被一号怪兽控制的区域也可以是2号怪兽。

7、复活过得地方不会在死一次了,并且起始点就算是复活地。

8、在到达终点的时候,体力值刚好变为0的话,不会再另外算一个回合,需要特殊判断。

9、如果不能到达终点的话,记得输出We need God's help!

10、对于消灭怪兽的情况需要用状态压缩。不然的话50*50的地图,你懂得。。。

接下去就祝大家好运了~~吐舌头


详见代码。

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;

int Map[110][110];
bool vis[60][60][50];//x,y,怪物被杀情况
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
int n,m,l;
int a[20],b[20],k;
int v[10];

struct node
{
    int x,y;
    int step;//剩余行动值
    int t;//回合数
    int flag;//表示哪位怪物被杀flag的二进制数哪一位等于1就说明哪只怪被杀
    //node() {}//可有可无
    //node(int x, int y, int t, int step, int flag):x(x), y(y), t(t), step(step), flag(flag) {}
    bool friend operator<(const node &a,const node &b)
    {
        if (a.t!=b.t)
            return a.t>b.t;
        return a.step<b.step;
    }
} s,ss;

int bfs(int sx,int sy,int ex,int ey)
{
    priority_queue<node>q,qq;
    memset(vis,false,sizeof(vis));
    memset(v,0,sizeof(v));
    s.x=sx;
    s.y=sy;
    s.step=l;
    s.flag=0;
    s.t=1;
    //if (Map[s.x][s.y]>0)
        //s.t++;
    vis[s.x][s.y][s.flag]=true;
    q.push(s);
    while(!q.empty())
    {
        s=q.top();
        q.pop();
        //cout<<s.x<<" "<<s.y<<" "<<s.step<<" "<<s.t<<" "<<s.flag<<endl;
        if (s.x==ex&&s.y==ey)
        {
            if (s.step==l)
                return s.t-1;
            return s.t;
        }
        for (int i=0; i<4; i++)
        {
            int X=s.x+dir[i][0];
            int Y=s.y+dir[i][1];
            //cout<<X<<" "<<Y<<endl;
            if (X>=1&&X<=n&&Y>=1&&Y<=m&&Map[X][Y]!=-1)
            {
                //cout<<X<<" "<<Y<<endl;
                ss.x=X;
                ss.y=Y;
                ss.flag=s.flag;
                ss.t=s.t;
                if (Map[X][Y]==0||(ss.flag&(1<<(Map[X][Y])))!=0)//判断该位置怪物是否被杀掉,杀掉为1
                    ss.step=s.step-1;//cout<<11111111111111<<ss.step<<endl;
                else
                    ss.step=0;
                for (int j=1; j<=k; j++)
                    if (X==a[j]&&Y==b[j])
                    {
                        ss.flag=s.flag|(1<<j);//把这个怪物杀掉
                        //break;
                    }
                //cout<<22222222<<" "<<X<<" "<<Y<<" "<<ss.step<<" "<<s.t<<endl;
                if (ss.step==0)//体力值等于0之后回合数加1
                    ss.t=s.t+1,ss.step=l;
                if (!vis[ss.x][ss.y][ss.flag])
                    q.push(ss),vis[ss.x][ss.y][ss.flag]=true;
            }
        }
    }
    return -1;
}

int main()
{
    while(~scanf("%d%d%d",&n,&m,&l))
    {
        for (int i=1; i<=n; i++)
        {
            for (int j=1; j<=m; j++)
            {
                scanf("%d",&Map[i][j]);
            }
        }
        scanf("%d",&k);
        for (int i=1; i<=k; i++)
        {
            scanf("%d%d",&a[i],&b[i]);
        }
        int x1,x2,y1,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        if (x1==x2&&y1==y2)
        {
            printf ("0\n");
            continue;
        }
        int ans=bfs(x1,y1,x2,y2);
        if (ans==-1)
            printf ("We need God's help!\n");
        else
            printf ("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(搜索,bfs)