HDU2128 Tempter of the Bone II BFS

Tempter of the Bone II

Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

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

题解

走迷宫问最短时间,不同的是这道题有炸弹这个因素,同样的BFS,不过节点要多加地图的保存和标志数组多加一个用来标志炸弹个数即可

代码

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define MAX 100000
#define LL long long
int cas=1,T;
struct Node
{
    int x,y;
    int num;    //炸弹个数
    int step;   //步数
    char mapp[8][8];
    bool operator<(const Node &p)const
    {
        return step>p.step;
    }
}node;

int net[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
char map[8][8];
int vis[8][8][577];
int main()
{
    int n,m,sx,sy,ex,ey;
    while (scanf("%d%d",&n,&m) && n)
    {
        priority_queue<Node>que;

        for (int i = 0;i<n;i++)
            scanf("%s",map[i]);
        for (int i = 0;i<n;i++)
        {
            for (int j = 0;j<m;j++)
            {
                if (map[i][j]=='S')
                {
                    map[i][j] = '.';
                    sx = i;sy =j;
                }
                else if (map[i][j]=='D')
                {
                    map[i][j]='.';
                    ex=i;ey=j;
                }
            }
        }

        memset(vis,0,sizeof(vis));
        node.x=sx;
        node.y=sy;

        for (int i = 0;i<n;i++)
            for (int j = 0;j<m;j++)
                node.mapp[i][j]=map[i][j];
        node.step=0;
        node.num=0;
        vis[sx][sy][0]++;
        que.push(node);
        while (!que.empty())
        {
            node = que.top();
            if (node.x==ex&&node.y==ey)
            {
                printf("%d\n",node.step);
                break;
            }

            for (int i = 0;i<4;i++)
            {
                node.step++;
                node.x+=net[i][0];
                node.y+=net[i][1];
                if (node.x>=0&&node.y>=0&&node.y<m&&node.x<n&&vis[node.x][node.y][node.num]<20)
                {
                    if (node.mapp[node.x][node.y]=='.')
                    {
                        vis[node.x][node.y][node.num]++;
                        que.push(node);
                    }
                    else if (node.mapp[node.x][node.y]=='X' && node.num>0)
                    {
                        vis[node.x][node.y][node.num]++;
                        node.step++;
                        node.num--;
                        node.mapp[node.x][node.y]='.';
                        vis[node.x][node.y][node.num]++;
                        que.push(node);
                    }
                    else if (node.mapp[node.x][node.y]>='1' && node.mapp[node.x][node.y]<='9')              
                    {
                        vis[node.x][node.y][node.num]++;
                        node.num+=node.mapp[node.x][node.y]-'0';
                        node.mapp[node.x][node.y]='.';
                        vis[node.x][node.y][node.num]++;
                        que.push(node);
                    }
                }
                node=que.top();
            }
            que.pop();
        }
        if (que.empty())
            printf("-1\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值