HDU-2128 Tempter of the Bone II 迭代加深搜索

Tempter of the Bone II

Time Limit: 10000/5000 MS (Java/Others)
Memory Limit: 98304/32768 K (Java/Others)

Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze was changed and the way he came in was lost.He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with the sizes of N by M. The maze is made up of a door,many walls and many explosives. Doggie need to reach the door to escape from the tempter. In every second, he could move one block to one of the upper, lower, left or right neighboring blocks. And if the destination is a wall, Doggie need another second explode and a explosive to explode it if he had some explosives. Once he entered a block with explosives,he can take away all of the explosives. Can the poor doggie survive? Please help him.

Input
The input consists of multiple test cases. The first line of each test case contains two integers N, M,(2 <= N, M <= 8). which denote the sizes of the maze.The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

‘X’: a block of wall;
‘S’: the start point of the doggie;
‘D’: the Door;
‘.’: an empty block;
‘1’–‘9’:explosives in that block.

Note,initially he had no explosives.

The input is terminated with two 0’s. This test case is not to be processed.

Output
For each test case, print the minimum time the doggie need to escape if the doggie can survive, or -1 otherwise.

Sample Input

4 4
SX..
XX..
….
1..D
4 4
S.X1
….
..XX
..XD
0 0

Sample Output

-1
9

有一句话,我必须得吐槽:炸墙还会耗费1s时间,题目中怎么好像没有描述,郁闷……
既然是最小步数的题型,那么首先很容易想到的就是bfs。
那么,每一个状态需要存入队列里的状态就包括了当前搜索的坐标,时间,以及炸掉的墙。
脑补了一下,觉得可能会爆空间,而且状压起来操作有点麻烦。所以有没有一个可以不需要保存地图的算法呢,这就需要结合回溯算法占用空间小的优点。
于是乎,ID算法就这么的横空出世了23333
depth的初始值应该要是出发点到出口的曼哈顿距离,然后进行迭代加深搜索,但是这样仍然有一个需要解决的问题,就是要怎么判断数据无解。因为n,m<=8,所以depth可以人为地加一个上界,达到了上界之后,就认为无解,至于上界究竟要取多少,就不一定了,太大耗时会变大,太小可能会搜成无解,大家自行把握咯~

运行结果
时间:0ms 空间:1692kb

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n,m,sx,sy,dx,dy,depth,b[10][10],v[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
char map[10][10];
bool found;
bool input()
{
    if(scanf("%d %d\n",&n,&m)==EOF||(n==0&&m==0))
      return false;
    for(int i=1;i<=n;i++,getchar())
      for(int j=1;j<=m;j++)
      {
        scanf("%c",&map[i][j]);
        if(map[i][j]=='S')
          sx=i,sy=j;
        else if(map[i][j]=='D')
          dx=i,dy=j;
      }
    depth=0;
    found=false;
    return true;
}
void dfs(int x,int y,int num,int s)
{
    if(s==depth)
    {
        if(map[x][y]=='D')
          found=true;
        return ;
    }
    if(abs(x-dx)+abs(y-dy)>depth-s)//曼哈顿距离剪枝
      return ;
    char ch;
    int temp;
    for(int i=0;i<4;i++)
    {
        int xx=x+v[i][0],yy=y+v[i][1];
        if(xx<1||xx>n||yy<1||yy>m||b[xx][yy]>=num)
          continue;
        if(map[xx][yy]=='X')
        {
            if(num==0) continue;
            temp=b[xx][yy];
            b[xx][yy]=num-1;
            map[xx][yy]='.';
            dfs(xx,yy,num-1,s+2);
            map[xx][yy]='X';
        }
        else if(map[xx][yy]>='1'&&map[xx][yy]<='9')
        {
            temp=b[xx][yy];
            ch=map[xx][yy];
            b[xx][yy]=num+ch-'0';
            map[xx][yy]='.';
            dfs(xx,yy,num+ch-'0',s+1);
            map[xx][yy]=ch;
        }
        else
        {
            temp=b[xx][yy];
            b[xx][yy]=num;
            dfs(xx,yy,num,s+1);
        }
        if(found) return ;//判重数组也应该要回溯
        b[xx][yy]=temp;//不然可能会把更优解剪掉
    }
}
void newer()
{
    for(int i=1;i<=n;i++)
      for(int j=1;j<=m;j++)
        b[i][j]=-1;
    b[sx][sy]=0;
    depth++;
    dfs(sx,sy,0,0);
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(input())
    {
        while(depth<=100&&!found)//别忘了定上界
          newer();
        if(found)
          printf("%d\n",depth);
        else
          printf("-1\n");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值