您的位置:首页 > 其它

hdu Nightmare(BFS||DFS)(不标记的剪枝)

2017-01-08 15:10 211 查看
Nightmare

Problem Description

Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent
the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area
in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes.

Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1.

Here are some rules:

1. We can assume the labyrinth is a 2 array.

2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too.

3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth.

4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb.

5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish.

6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.

 

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.

Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth.

There are five integers which indicate the different type of area in the labyrinth:

0: The area is a wall, Ignatius should not walk on it.

1: The area contains nothing, Ignatiu
4000
s can walk on it.

2: Ignatius' start position, Ignatius starts his escape from this position.

3: The exit of the labyrinth, Ignatius' target position.

4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.

 

Output

For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1.

 

Sample Input

3
3 3
2 1 1
1 1 0
1 1 3
4 8
2 1 1 0 1 1 1 0
1 0 4 1 1 0 4 1
1 0 0 0 0 0 0 1
1 1 1 4 1 1 1 3
5 8
1 2 1 1 1 1 1 4
1 0 0 0 1 0 0 1
1 4 1 0 1 1 0 1
1 0 0 0 0 3 0 1
1 1 4 1 1 1 1 1

 

Sample Output

4
-1
13

思路:
根据前后两次踏上同一位置离爆炸时间的长短来标记。就是说,如果第二次踏上一个位置时,离爆炸的时间短了并且步数又增加了,那么这条路很明显不是最优的了。那就根据这个来剪枝了。
ps:
这道题又坑了我好长时间啊0.0,一开始想到BFS,碰到4时直接消除所有的记录,然后想了想发现并不行。然后又让它记录路径,碰到4时,消除以前走过的路的记录,结果发现正确的路还是走不通。然后又想着可以预判有4的那个点能不能走(如果走过去再回来时间变多了就可以走),结果发现如果终点在4后边,就又走不通了。然后换DFS吧,发现写了结果还是超时。0.0.。。。最后看了下大神的思路才明白
DFS代码:
#include<stdio.h>
#include<string.h>
#define min(a,b) (a<b?a:b)
int time[10][10];//标记到这一地点离爆炸的时间
int step[10][10];//标记到这一地点所用的步数
int s[10][10];
int to[][2]= {0,1,0,-1,1,0,-1,0};
int n,m,ans;
void dfs(int x,int y,int cnt,int ti)
{
if(x<0||x>=n||y<0||y>=m||cnt>=ans||ti<=0||s[x][y]==0)//剪枝
return ;
if(s[x][y]==3)//找到目标
{
ans=min(ans,cnt);
return ;
}
if(s[x][y]==4)
ti=6;
if(cnt>=step[x][y]&&ti<=time[x][y])//到此点当前所用的步数>=其他点到此点的步数,并且离爆炸的时间还变短了,那么此路肯定不优
return ;//这是因为结点可重复访问,但是本身没标记,那么当上述条件满足时,(x,y)点的最优解已经求过(存在或者不存在),所以不优的可以直接筛掉。
step[x][y]=cnt,time[x][y]=ti;
for(int i=0; i<4; i++)
{
int xx=x+to[i][0],yy=y+to[i][1];
dfs(xx,yy,cnt+1,ti-1);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
ans=64;
int i,j,x,y;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
{
scanf("%d",&s[i][j]);
time[i][j]=0;//初始化
step[i][j]=64;//初始化
if(s[i][j]==2)
x=i,y=j;
}
dfs(x,y,0,6);
if(ans==64)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}


BFS代码(队友想的,我写了一下):
#include<stdio.h>
#include<queue>
using namespace std;
int s[10][10];
int to[][2]= {0,1,0,-1,1,0,-1,0};
int n,m;
struct node
{
int x,y,step,ti;
};
int bfs(int x,int y)
{
node now,next;
queue<node>q;
now.x=x,now.y=y,now.step=0,now.ti=6;
q.push(now);
while(!q.empty())
{
now=q.front();
if(now.ti<=0)
{
q.pop();
continue;
}
if(now.step>64)
return -1;
if(s[now.x][now.y]==3)
return now.step;
for(int i=0; i<4; i++)
{
int xx=now.x+to[i][0],yy=now.y+to[i][1];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&s[xx][yy])
{
next.ti=now.ti-1;
if(s[xx][yy]==4&&next.ti>0)
{
next.ti=6;
s[xx][yy]=0;//为4的点走第二次并没有用
}
next.x=xx,next.y=yy,next.step=now.step+1;
q.push(next);
}
}
q.pop();
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int i,j,x,y;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
{
scanf("%d",&s[i][j]);
if(s[i][j]==2)
x=i,y=j;
}
int ans=bfs(x,y);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: