Maze zoj3652(bfs+记忆化+状态压缩)

来源:互联网 发布:淘宝哪家茶叶店好 编辑:程序博客网 时间:2024/06/13 01:10

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 rectangle of n*m, and Celica is at (x1y1) at the beginning while he needs to go to (x2y2). 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 (x2y2). 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, nml.(1≤nm≤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 peuqals 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 x1y1x2y2.

Output

If Celica can't get to the (x2y2), 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.


思路:没什么好说的,直接记录下状态,bfs+记忆化。如果用优先队列的话,可能能够降低耗时。

代码:

#include <cstdio>#include <cstring>#include <queue>using namespace std;const long M=110;const long d[4][2]={{1,0},{0,1},{-1,0},{0,-1}};long n,m,l,k,mat[M][M],pos[M][M];long dp[M][M][1<<6],sx,sy,ex,ey,ans;struct node{long x,y,state,t;node(long x,long y,long state,long t):x(x),y(y),state(state),t(t){}};queue<node>Q;void bfs(long sx,long sy){dp[sx][sy][0]=0;Q.push(node(sx,sy,0,0));while (!Q.empty()){long x=Q.front().x,y=Q.front().y;long state=Q.front().state,t=Q.front().t;Q.pop();for (long i=0;i<4;++i){long dx=x+d[i][0],dy=y+d[i][1];long dstate=state,dt=t+1;if (!dx || !dy || dx>n || dy>m || mat[dx][dy]==-1) continue;if (mat[dx][dy] && ((dstate & (1<<mat[dx][dy]))==0))while (dt%l) dt++;if (pos[dx][dy]) dstate=dstate | (1<<pos[dx][dy]);if (dt<dp[dx][dy][dstate] || dp[dx][dy][dstate]==-1){dp[dx][dy][dstate]=dt;if (dx==ex && dy==ey) ans=min(ans,dt);else Q.push(node(dx,dy,dstate,dt));}}}}int main(){while (~scanf("%d%d%d",&n,&m,&l)){for (long i=1;i<=n;++i)for (long j=1;j<=m;++j)scanf("%d",&mat[i][j]);scanf("%d",&k);memset(pos,0,sizeof(pos));for (long i=1;i<=k;++i){long x,y;scanf("%d%d",&x,&y);pos[x][y]=i;}scanf("%d%d%d%d",&sx,&sy,&ex,&ey);memset(dp,-1,sizeof(dp));ans=1<<30;bfs(sx,sy);if (ans==(1<<30)) printf("We need God's help!\n");else{if (ans%l) ans=ans/l+1;else ans=ans/l;printf("%d\n",ans);}}return 0;}




原创粉丝点击