HDU3681Prison Break(状态压缩+BFS)

Prison Break

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

Total Submission(s): 3165    Accepted Submission(s): 804

Problem Description

Rompire is a robot kingdom and a lot of robots live there peacefully. But one day, the king of Rompire was captured by human beings. His thinking circuit was changed by human and thus became a tyrant. All those who are against him
were put into jail, including our clever Micheal#1. Now it’s time to escape, but Micheal#1 needs an optimal plan and he contacts you, one of his human friends, for help.

The jail area is a rectangle contains n×m little grids, each grid might be one of the following:

1) Empty area, represented by a capital letter ‘S’.

2) The starting position of Micheal#1, represented by a capital letter ‘F’.

3) Energy pool, represented by a capital letter ‘G’. When entering an energy pool, Micheal#1 can use it to charge his battery ONLY ONCE. After the charging, Micheal#1’s battery will become FULL and the energy pool will become an empty area. Of course, passing
an energy pool without using it is allowed.

4) Laser sensor, represented by a capital letter ‘D’. Since it is extremely sensitive, Micheal#1 cannot step into a grid with a laser sensor.

5) Power switch, represented by a capital letter ‘Y’. Once Micheal#1 steps into a grid with a Power switch, he will certainly turn it off.

In order to escape from the jail, Micheal#1 need to turn off all the power switches to stop the electric web on the roof—then he can just fly away. Moving to an adjacent grid (directly up, down, left or right) will cost 1 unit of energy and only moving operation
costs energy. Of course, Micheal#1 cannot move when his battery contains no energy.

The larger the battery is, the more energy it can save. But larger battery means more weight and higher probability of being found by the weight sensor. So Micheal#1 needs to make his battery as small as possible, and still large enough to hold all energy he
need. Assuming that the size of the battery equals to maximum units of energy that can be saved in the battery, and Micheal#1 is fully charged at the beginning, Please tell him the minimum size of the battery needed for his Prison break.

Input

Input contains multiple test cases, ended by 0 0. For each test case, the first line contains two integer numbers n and m showing the size of the jail. Next n lines consist of m capital letters each, which stands for the description
of the jail.You can assume that 1<=n,m<=15, and the sum of energy pools and power switches is less than 15.

Output

For each test case, output one integer in a line, representing the minimum size of the battery Micheal#1 needs. If Micheal#1 can’t escape, output -1.

Sample Input

5 5
GDDSS
SSSFS
SYGYS
SGSYS
SSYSS
0 0

Sample Output

4

Source

2010 Asia Hangzhou Regional Contest

题意:一个人背着一个有一定容量的电池,每走一步耗用1,D:不能走,S:空地 ,G:能量池,把背的容量电池充满(只能用一次)可取可不取,F:走点,Y:开关,这个人必须全部关闭开关才能走出地牢,问最小背多大的容量电池才能走出。

注意:在一个位置,虽然某一个状态走过了,但当前剩余能量比前一次走过的能量大,则也要放入队列中。

解题:先求出最大容量,不用管G,再用二分+bfs出最小须要的容量。

#include<iostream>
#include<queue>
using namespace std;
typedef struct nnn
{
    int x,y,G,Y,st;
}node;
char map[16][16];
int vist[16][16][1<<16];
int n,m,sta_y,k,sx,sy,dir[4][2]={0,1,0,-1,1,0,-1,0},tmap[16][16];
void init()
{
    for(int i=0;i<n;i++)
    for(int j=0;j<m;j++)
    for(int s=0;s<(1<<k);s++)
    vist[i][j][s]=-1;
}
int bfs()
{
    if(sta_y==0)return 0;
    queue<node>q;
    node p,pp;
    init();
    p.x=sx; p.y=sy; p.Y=0; p.st=0;
    vist[sx][sy][0]=1;
    q.push(p);
    while(!q.empty())
    {
        p=q.front(); q.pop();
        for(int e=0;e<4;e++)
        {
            int tx,ty;
            tx=p.x+dir[e][0]; ty=p.y+dir[e][1];
            if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]!='D')
            {
                pp.x=tx; pp.y=ty; pp.st=p.st+1; pp.Y=p.Y;
                if(map[tx][ty]=='Y')pp.Y|=(1<<tmap[tx][ty]);
                if(pp.Y==sta_y)return pp.st;
               if(vist[tx][ty][pp.Y]==-1)
                q.push(pp),vist[tx][ty][pp.Y]=1;
            }
        }
    }
    return -1;
}
int bfs1(int maxst)
{
    queue<node>q;
    node p,pp;
    init();
    p.x=sx; p.y=sy; p.G=0; p.Y=0; p.st=maxst;
    vist[sx][sy][0]=maxst;//注意
    q.push(p);
    while(!q.empty())
    {
        p=q.front(); q.pop();
        for(int e=0;e<4;e++)
        {
            int tx,ty;
            tx=p.x+dir[e][0]; ty=p.y+dir[e][1];
            if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]!='D')
            {
                pp.x=tx; pp.y=ty; pp.st=p.st-1; pp.Y=p.Y; pp.G=p.G;
                if(map[tx][ty]=='Y')
                {
                    pp.Y|=(1<<tmap[tx][ty]);
                    if(pp.Y==sta_y)return 1;
                }
                else if(map[tx][ty]=='G')
                {
                   if((pp.G&(1<<tmap[tx][ty]))==0)
                   {
                       pp.G|=(1<<tmap[tx][ty]); pp.st=maxst;
                       if(vist[tx][ty][pp.G|pp.Y]<maxst)//注意
                        q.push(pp); vist[tx][ty][pp.G|pp.Y]=maxst;
                   }
                    pp.G=p.G; pp.st=p.st-1;
                }
                if(pp.st&&vist[tx][ty][pp.G|pp.Y]<pp.st)//注意
                 q.push(pp),vist[tx][ty][pp.G|pp.Y]=pp.st;
            }
        }
    }
    return 0;
}
int main()
{
    int minst;
    while(scanf("%d%d",&n,&m)>0&&m+n!=0)
    {
        for(int i=0;i<n;i++)scanf("%s",map[i]);
        k=0; sta_y=0;
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        if(map[i][j]=='G')
        {
            tmap[i][j]=k; k++;
        }
        else if(map[i][j]=='Y')
        {
            tmap[i][j]=k; sta_y|=(1<<k); k++;
        }
        else if(map[i][j]=='F')
        sx=i,sy=j;

        minst=bfs();
        if(minst<=1)
        {
            printf("%d\n",minst);continue;
        }
        int l,mm;
        l=1;
        while(l<minst)
        {
            mm=(l+minst)/2;
            if(bfs1(mm)==1)minst=mm;
            else l=mm+1;
        }
        printf("%d\n",minst);
    }
}
时间: 2024-10-12 11:27:14

HDU3681Prison Break(状态压缩+BFS)的相关文章

hdu 3681 Prison Break(状态压缩+bfs)

Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one day, the king of Rompire was captured by human beings. His thinking circuit was changed by human and thus became a tyrant. All those who are against him

胜利大逃亡(续)(状态压缩bfs)

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

2014 Super Training #6 G Trim the Nails --状态压缩+BFS

原题: ZOJ 3675 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3675 由m<=20可知,可用一个二进制数表示指甲的状态,最多2^20,初始状态为0,表示指甲都没剪,然后BFS找解,每次枚举剪刀的两个方向,枚举移动的位数进行扩展状态即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include &

hdu1429胜利大逃亡(续) (状态压缩+BFS)

Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)-- 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置.Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个.魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去.经过若干次的尝试,Ignatius已画

hdu 4771 Stealing Harry Potter&#39;s Precious (状态压缩+bfs)

Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1297    Accepted Submission(s): 619 Problem Description Harry Potter has some precious. For example, his invisib

hdu1885Key Task(状态压缩+bfs)

题目链接: 啊哈哈,点我点我 这题和hdu1429是姊妹题  请参见传送门 题目: Key Task Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1187    Accepted Submission(s): 470 Problem Description The Czech Technical University is rath

hdu1429胜利大逃亡(续)(状态压缩+bfs)

题目链接: 啊哈哈,点我点我 题意及思路 最开始我以为跟普通的bfs一样,所以直接写了一个朴素的bfs,一跑,前两组数据对了,但是第三组不对,一看,走过的还可以走啊,所以不能标记,结果我的bfs乱改,最后 毫无疑问改成了死循环.所以看题解... 思路:因为有10中不同的钥匙,每种都有两种状态,所以结合计算机是二进制保存的特点,刚好把这10把钥匙当成每一个为,要要1<<10个位保存所有的状态,然后就是模拟捡起钥匙,捡起钥匙就是说明这个位上的数字变成1这个状态,所以自然而然想到了位运算,只要|一下

hdu5094 状态压缩+bfs

http://acm.hdu.edu.cn/showproblem.php?pid=5094 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of Starship Enterprise, fell into Klingon's trick and was held as prisoner on their mother planet Qo'noS.

POJ 1324 Holedox Moving 贪吃蛇 状态压缩 BFS

Description During winter, the most hungry and severe time, Holedox sleeps in its lair. When spring comes, Holedox wakes up, moves to the exit of its lair, comes out, and begins its new life. Holedox is a special snake, but its body is not very long.