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 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

题意:要求从 S 点走到 D 点的最短时间,‘1’~‘9’:表示当前位置有炸药数量,X:表示墙。" . ":表示可走的路。墙可以用一包炸药炸通,需花一个单位时间。

分析:从S到D,所经过的每个位置得到的炸药数量可能不等,就算得到的炸药数量相等,但每个到达当前位置时的地图的状态不同。所以我们不仅必须判断在每个位置得到的炸药数是否存在过,而且还要判断得到的这些炸药数在相同情况下不同地图的状态。

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

struct State
{
    short state[8];//地图每行压缩的状态,
};
struct node
{
    int x,y; //当前走到的位置
    int time,explosiveNumb; //走过的时间,得到的炸药数量
    State s;  //走过后的地图状态
    friend bool operator<(const node &a,const node &b) //优先队列以时间最小的顺序取
    {
        return a.time>b.time;
    }
};

int n,m;
char map[10][10];
vector<State>vist[8][8][560]; //走到地图每个位置得到不同炸药数量的地图状态有没有存在过

void init()
{
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
        for(int k=0; k<559; k++)
        vist[i][j][k].clear();
}
bool judge(int x,int y,int numb,short state[])
{
    int length=vist[x][y][numb].size();
    for(int i = 0; i < length ;i++)
    {
        int j;
        for(j = 0; j<n ; j++)
            if(vist[x][y][numb][i].state[j]!=state[j])
            break;
        if(j==n) //当前将要走的state[]地图状态,不必再加入队列(即己经存在过)
            return false;
    }
    //当前将要走的地图状态没存在过,需加入队列
    return true;
}
int bfs(int sx,int sy)
{
    int dir[4][2]={0,1,0,-1,1,0,-1,0};
    priority_queue<node>q;
    node p,tp;

    init();
    p.x=sx;
    p.y=sy;
    p.time=0;
    p.explosiveNumb=0;
    for(int i=0;i<n;i++)
    {
        p.s.state[i]=0;
        //压缩每行地图的状态,位置是 1 表示不存在任何东西
        for(int j=0;j<m;j++)
            if(map[i][j]=='.')
        p.s.state[i]|=(1<<j);
    }

    vist[sx][sy][0].push_back(p.s);
    q.push(p);
    while(!q.empty())
    {
        p=q.top();
        q.pop();
        for(int e=0;e<4;e++)
        {
            tp=p;
            tp.x+=dir[e][0];
            tp.y+=dir[e][1];
            tp.time++;
            if(tp.x>=0&&tp.x<n&&tp.y>=0&&tp.y<m)
            {
                if(!(tp.s.state[tp.x]&(1<<tp.y))&&map[tp.x][tp.y]=='X'&&!tp.explosiveNumb) //表示将要走的位置是现在还没炸的墙,并且身上没有炸药
                    continue ;
                if(!(tp.s.state[tp.x]&(1<<tp.y))&&map[tp.x][tp.y]>'0'&&map[tp.x][tp.y]<='9') //表示将要走的位置是没有取走的炸药
                {
                    tp.explosiveNumb+=map[tp.x][tp.y]-'0';
                    tp.s.state[tp.x]|=1<<tp.y; //取走后变成 "."
                }
                else if(!(tp.s.state[tp.x]&(1<<tp.y))&&map[tp.x][tp.y]=='X'&&tp.explosiveNumb)  //表示将要走的位置是现在还没炸的墙,身上有炸药
                {
                    tp.explosiveNumb--;
                    tp.s.state[tp.x]|=1<<tp.y; //炸完后变成 "."
                    tp.time++;
                }
                else if(map[tp.x][tp.y]=='D')
                    return tp.time;
                if(judge(tp.x,tp.y,tp.explosiveNumb,tp.s.state)) //判断走到当前位置的炸药数量与地图状态是否没有存在过?为真没存在过,否则为存在过
                vist[tp.x][tp.y][tp.explosiveNumb].push_back(tp.s),q.push(tp);
            }
        }
    }
    return -1;
}
int main()
{
    int sx,sy;
    while(scanf("%d%d",&n,&m)>0)
    {
        if(n==0&&m==0)
            break;
        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')
            sx=i,sy=j;
        printf("%d\n",bfs(sx,sy));
    }
}
时间: 2024-10-23 02:18:47

HDU2128Tempter of the Bone II(bfs+状态压缩)的相关文章

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

hdu--1429--胜利大逃亡(续) (bfs+状态压缩)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8560    Accepted Submission(s): 3071 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)-- 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带

ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其到(1,1)的最短路长 题解:开始做的时候用BFS暴力做了一次,结果RE了,后来看了其他的题解和discuss才转向状态压缩.也看到有人用A*做出来了. 现在简要介绍一下状态压缩的思路: 由于蛇身最长只有8,可以利用两条相邻蛇身坐标确定其相对方向(四个方向),两位二进制可以表示 这样 一个蛇头坐标+

hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 901    Accepted Submission(s): 314 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of St

hdu2209翻纸牌游戏(bfs+状态压缩)

Problem Description 有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌.但是麻烦的是,每当你翻一张纸牌(由正翻到反,或者有反翻到正)时,他左右两张纸牌(最左边和最右边的纸牌,只会影响附近一张)也必须跟着翻动,现在给你一个乱的状态,问你能否把他们整理好,使得每张纸牌都正面朝上,如果可以,最少需要多少次操作. Input 有多个case,每个case输入一行01符号串(长度不超过20),1表

poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you f

hdu 4856 Tunnels(bfs+状态压缩)

题目链接:hdu 4856 Tunnels 题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,现在有人想要体验下这M个管道,问最短需要移动的距离,起点未定. 解题思路:首先用bfs处理出两两管道之间移动的距离,然后后用状态压缩求出最短代价,dp[i][j],i表示的已经走过的管道,j是当前所在的管道. #include <cstdio> #include <cstring> #include <queue> #include <algorithm&g

HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP 用最短路预处理出状态的转移.能够优化非常多 AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <queue> us

hdu1254推箱子 (BFS+BFS+状态压缩)

推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5089 Accepted Submission(s): 1421 Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱