HDU-2128-Tempter of the Bone II(BFS)

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

Author

XHD

Source

HDU 2007-10 Programming Contest

思路:因为地图会变化,结构体里面要加一个数组保存地图,标记的话就标记根据地图的坐标和当前手中的炸药数量来标记,但是同样状态的标记可能会有不同的走法,所以用整形变量来存,并且设定一个状态可以访问的阀值,这里设为20。

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

struct S{
int x,y,num,step;
char mp[8][8];
bool operator<(const S & p) const
{
    return step>p.step;
}
}t;

int nxt[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
char s[8][9];
int vis[8][8][577];

int main()
{
    int n,m,i,j,sx,sy,ex,ey;

    while(~scanf("%d%d",&n,&m) && n)
    {
        priority_queue<S>que;

        for(i=0;i<n;i++) scanf("%s",s[i]);

        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(s[i][j]=='S')
                {
                    s[i][j]='.';

                    sx=i,sy=j;
                }
                else if(s[i][j]=='D')
                {
                    s[i][j]='.';

                    ex=i,ey=j;
                }
            }
        }

        memset(vis,0,sizeof vis);

        t.x=sx;
        t.y=sy;

        for(i=0;i<n;i++) for(j=0;j<m;j++) t.mp[i][j]=s[i][j];

        t.step=0;
        t.num=0;

        vis[sx][sy][0]++;

        que.push(t);

        while(!que.empty())
        {
            t=que.top();

            if(t.x==ex && t.y==ey)
            {
                printf("%d\n",t.step);

                break;
            }

            for(i=0;i<4;i++)
            {
                t.step++;
                t.x+=nxt[i][0];
                t.y+=nxt[i][1];

                if(t.x>=0 && t.x<n && t.y>=0 && t.y<m && vis[t.x][t.y][t.num]<20)
                {
                    if(t.mp[t.x][t.y]=='.')
                    {
                        vis[t.x][t.y][t.num]++;

                        que.push(t);
                    }
                    else if(t.mp[t.x][t.y]=='X' && t.num>0)
                    {
                        vis[t.x][t.y][t.num]++;
                        t.step++;
                        t.num--;
                        t.mp[t.x][t.y]='.';
                        vis[t.x][t.y][t.num]++;

                        que.push(t);
                    }
                    else if(t.mp[t.x][t.y]>='1' && t.mp[t.x][t.y]<='9')
                    {
                        vis[t.x][t.y][t.num]++;
                        t.num+=t.mp[t.x][t.y]-'0';
                        t.mp[t.x][t.y]='.';
                        vis[t.x][t.y][t.num]++;

                        que.push(t);
                    }
                }
                 t=que.top();
            }
            que.pop();
        }

        if(que.empty()) printf("-1\n");
    }
}

HDU-2128-Tempter of the Bone II(BFS),布布扣,bubuko.com

时间: 2024-10-23 02:18:44

HDU-2128-Tempter of the Bone II(BFS)的相关文章

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) Total Submission(s): 1638    Accepted Submission(s): 435 Problem Description The doggie found a bone in an ancient maze, which fascinated him

HDU2128Tempter of the Bone II(bfs+状态压缩)

Tempter of the Bone II Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 98304/32768 K (Java/Others) Total Submission(s): 1529    Accepted Submission(s): 407 Problem Description The doggie found a bone in an ancient maze, which fascinated him

【深搜加剪枝五】HDU 1010 Tempter of the Bone

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64326    Accepted Submission(s): 17567 Problem Description The doggie found a bone in an ancient maze, which fascinated him a l

HDU 1010 Tempter of the Bone heuristic 剪枝法

本题就是考剪枝法了. 应该说是比较高级的应用了.因为要使用heuristic(经验)剪枝法.要总结出这个经验规律来,不容易.我说这是高级的应用也因为网上太多解题报告都没有分析好这题,给出的程序也很慢,仅仅能过掉,由此看来很多人没有做好这道题. 这里我需要更正一下网上流行的说法:奇偶剪枝法. 其实本题使用奇偶剪枝法并不能太大提高速度,只能说仅仅让使用奇偶剪枝过掉.所以网上说本题使用奇偶剪枝的,其实并不能提高速度. 原因: 奇偶剪枝只能剪枝一次,不能在递归的时候剪枝,因为只要初始化位置符合奇偶性,那

HDU 1010 Tempter of the Bone dfs+剪枝

给你一个迷宫一个起点和一个终点,问你能否走T步刚好到达终点,不能重复走,并且只有4个方向 显然这是一个dfs,虽然N最大只有7,但是裸的dfs复杂度还是太高了,因此要进行一些剪枝 1.如果T比图上所有的可走点还要大,肯定是不可行的.这个可以避免dfs整张图. 2.奇偶剪枝,有性质当前点(x,y)到目标点(tx,ty)的所有路径的长度的奇偶性一定和|x-tx|+|y-ty|一样. #include <cstdio> #include <iostream> #include <c

hdu 1010 Tempter of the Bone (DFS+剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 68206    Accepted Submission(s): 18719 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of the Bone(DFS+奇偶性剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 70665    Accepted Submission(s): 19487 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of the Bone(bfs)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 97219    Accepted Submission(s): 26383 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of the Bone (DFS 奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 75141    Accepted Submission(s): 20531 Problem Description The doggie found a bone in an ancient maze, which fascinated him a