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 of
l. 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 become
l 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. The
j-th integer p in the line i describe the lattice in the
i line and j row. If p euqals to -1, it means you can't get into it. If
p euqals to 0, it means the lattice is not dominated by any monster. If
p 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 the
i-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.

思路:水搜索,注意细节即可。

#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std; struct S{
int x,y,step,state;
}t,qt; int mp[50][50],monx[10],mony[10],nxt[4][2]={{1,0},{0,1},{-1,0},{0,-1}},n,m,l,k;
bool v[1<<5][50][50],vis[1<<5][50][50]; queue<S>que,q; void span(int x,int y,int step,int state)
{
int i,j,temp; for(int lxd=0;lxd<(1<<k);lxd++) for(i=0;i<n;i++) for(j=0;j<m;j++) v[lxd][i][j]=0; qt.x=x;
qt.y=y;
qt.step=l;
qt.state=state; q.push(qt); while(!q.empty())
{
qt=q.front();
q.pop(); temp=qt.state; for(i=0;i<4;i++)
{
qt.x+=nxt[i][0];
qt.y+=nxt[i][1]; qt.state=temp; if(qt.x>=0 && qt.x<n && qt.y>=0 && qt.y<m && mp[qt.x][qt.y]>=0 && !v[qt.state][qt.x][qt.y])
{
v[qt.state][qt.x][qt.y]=1; for(j=0;j<k;j++) if(qt.x==monx[j] && qt.y==mony[j]) break; if(j==k)//没有妖怪
{
if(mp[qt.x][qt.y]==0)
{
qt.step--;
if(qt.step>0) q.push(qt);
qt.step++; t.x=qt.x;
t.y=qt.y;
t.step=step+1;
t.state=qt.state; if(!vis[t.state][t.x][t.y])
{
vis[t.state][t.x][t.y]=1;
que.push(t);
}
}
else
{
if((1<<mp[qt.x][qt.y]-1)&qt.state)
{
qt.step--;
if(qt.step>0) q.push(qt);
qt.step++; t.x=qt.x;
t.y=qt.y;
t.step=step+1;
t.state=qt.state; if(!vis[t.state][t.x][t.y])
{
vis[t.state][t.x][t.y]=1;
que.push(t);
}
}
else
{
t.x=qt.x;
t.y=qt.y;
t.step=step+1;
t.state=qt.state; if(!vis[t.state][t.x][t.y])
{
vis[t.state][t.x][t.y]=1;
que.push(t);
}
}
}
}
else//有妖怪
{
if(mp[qt.x][qt.y]==0)
{
qt.step--;
qt.state|=(1<<j);
v[qt.state][qt.x][qt.y]=1;
if(qt.step>0) q.push(qt);
qt.step++; t.x=qt.x;
t.y=qt.y;
t.step=step+1;
t.state=(qt.state|(1<<j)); if(!vis[t.state][t.x][t.y])
{
vis[t.state][t.x][t.y]=1;
que.push(t);
}
}
else
{
if((1<<mp[qt.x][qt.y]-1)&qt.state)
{
qt.step--;
qt.state|=(1<<j);
v[qt.state][qt.x][qt.y]=1;
if(qt.step>0) q.push(qt);
qt.step++; t.x=qt.x;
t.y=qt.y;
t.step=step+1;
t.state=(qt.state|(1<<j)); if(!vis[t.state][t.x][t.y])
{
vis[t.state][t.x][t.y]=1;
que.push(t);
}
}
else
{
t.x=qt.x;
t.y=qt.y;
t.step=step+1;
v[qt.state][qt.x][qt.y]=1;
t.state=(qt.state|(1<<j)); if(!vis[t.state][t.x][t.y])
{
vis[t.state][t.x][t.y]=1;
que.push(t);
}
}
}
}
} qt.x-=nxt[i][0];
qt.y-=nxt[i][1];
}
}
} int main()
{
int i,j,x1,x2,y1,y2; while(~scanf("%d%d%d",&n,&m,&l))
{
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&mp[i][j]);
}
} scanf("%d",&k); for(i=0;i<k;i++)
{
scanf("%d%d",&monx[i],&mony[i]); monx[i]--;
mony[i]--;
} scanf("%d%d%d%d",&x1,&y1,&x2,&y2); x1--;
x2--;
y1--;
y2--; while(!que.empty()) que.pop(); for(int lxd=0;lxd<(1<<k);lxd++) for(i=0;i<n;i++) for(j=0;j<m;j++) vis[lxd][i][j]=0; t.x=x1;
t.y=y1;
t.step=0;
t.state=0; vis[0][t.x][t.y]=1; que.push(t); while(!que.empty())
{
t=que.front(); if(t.x==x2 && t.y==y2)
{
printf("%d\n",t.step);
break;
} que.pop(); span(t.x,t.y,t.step,t.state);
} if(que.empty()) puts("We need God's help!");
}
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

ZOJ-3652-Maze(BFS)的更多相关文章

  1. ZOJ 3652 Maze 模拟,bfs,读题 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4842 要注意题目中两点: 1.在踏入妖怪控制的区域那一刹那,先减行动力,然后才 ...

  2. zoj 3652 Maze

    Maze Time Limit: 2 Seconds      Memory Limit: 65536 KB Celica is a brave person and believer of a Go ...

  3. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  4. POJ3026——Borg Maze(BFS+最小生成树)

    Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...

  5. POJ 3026 Borg Maze bfs+Kruskal

    题目链接:http://poj.org/problem?id=3026 感觉英语比题目本身难,其实就是个最小生成树,不过要先bfs算出任意两点的权值. #include <stdio.h> ...

  6. Borg Maze(bfs+prim)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6971   Accepted: 2345 Description The B ...

  7. LightOJ 1337 F - The Crystal Maze (bfs)

    Description You are in a plane and you are about to be dropped with a parasuit in a crystal maze. As ...

  8. POJ3026 Borg Maze(bfs求边+最小生成树)

    Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of ...

  9. CodeForces - 377A Maze BFS逆思维

    Maze Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, ...

  10. POJ - 3026 Borg Maze BFS加最小生成树

    Borg Maze 题意: 题目我一开始一直读不懂.有一个会分身的人,要在一个地图中踩到所有的A,这个人可以在出发地或者A点任意分身,问最少要走几步,这个人可以踩遍地图中所有的A点. 思路: 感觉就算 ...

随机推荐

  1. Linux学习笔记——例说makefile 头文件查找路径

    0.前言     从学习C语言開始就慢慢開始接触makefile,查阅了非常多的makefile的资料但总感觉没有真正掌握makefile,假设自己动手写一个makefile总认为非常吃力.所以特意借 ...

  2. GNU名称解析

    GNU它是GNU's NOT UNIX缩写G     N    U缩写,和GNU全名GNU's NOT UNIX 中间 GNU 也GNU's NOT UNIX缩写,它使用递归方式定义GNU.

  3. WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体

    原文:WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体 运行结果: 事实上很简单,定义好一个正方体,处理好纹理.关于MeshGeometry3D的正确定义和纹理这里就不多讲 ...

  4. [转载][NAS] 使用win8的“存储池”功能~

    之前自己用DQ77KB搭建一个小存储系统(帖子链接:http://www.chiphell.com/thread-567753-1-1.html),一直使用intel主板带的软RAID功能构建RAID ...

  5. 同一个PC只能运行一个应用实例(考虑多个用户会话情况)

    原文:同一个PC只能运行一个应用实例(考虑多个用户会话情况) class Program { private static Mutex m; [STAThread] static void Main( ...

  6. 乐在其中设计模式(C#) - 代理模式(Proxy Pattern)

    原文:乐在其中设计模式(C#) - 代理模式(Proxy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 代理模式(Proxy Pattern) 作者:webabcd 介绍 为 ...

  7. C#访问Java的WebService添加SOAPHeader验证的问题

    原文:C#访问Java的WebService添加SOAPHeader验证的问题 这两天做与公司OA的接口,发现C#访问Java的WebService需要提供一个SOAP的头验证信息,但是WebServ ...

  8. 深入理解JavaScript系列(33):设计模式之策略模式(转)

    介绍 策略模式定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化不会影响到使用算法的客户. 正文 在理解策略模式之前,我们先来一个例子,一般情况下,如果我们要做数据合法性验证,很 ...

  9. Apple Watch 1.0 开发介绍 1.2 简介 配置Xcode工程

    WatchKit app需要一个现有的iOS app.在iOS app工程中,添加一个新的WatchKit app target,它包含了WatchKit app和WatchKit extension ...

  10. 【C语言探索之旅】 第二部分第三课:数组

    内容简介 1.课程大纲 2.第二部分第三课: 数组 3.第二部分第四课预告:字符串 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏. C语 ...