HDU-3681-Prison Break(BFS+状压DP+二分)

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

思路:先用BFS预处理出F、G、Y之间的距离,然后二分结果用状压DP验证结果是否可行即可 。

#include <stdio.h>
#define max(A,B)(A>B?A:B)
#define INF 999999999

struct S{
int x,y,step;
}que[1000000],t;

char mp[15][20];
int n,m,dis[15][15],d[20][20],type[20],pos[20],cnt,ok,dp[1<<16][20],nxt[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool vis[15][15];

void bfs(int sx,int sy)
{
    int i,j,top=0,bottom=1;

    for(i=0;i<n;i++) for(j=0;j<m;j++) vis[i][j]=0,dis[i][j]=INF;

    que[0].x=sx;
    que[0].y=sy;
    que[0].step=0;

    dis[sx][sy]=0;
    vis[sx][sy]=1;

    while(top<bottom)
    {
        t=que[top];

        t.step++;

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

            if(t.x>=0 && t.x<n && t.y>=0 && t.y<m && mp[t.x][t.y]!='D' && !vis[t.x][t.y])
            {
                vis[t.x][t.y]=1;

                dis[t.x][t.y]=t.step;

                que[bottom++]=t;
            }

            t.x-=nxt[i][0];
            t.y-=nxt[i][1];
        }

        top++;
    }
}

bool check(int x)
{
    int i,j,k;

    for(i=0;i<(1<<cnt);i++) for(j=0;j<cnt;j++) dp[i][j]=-1;

    for(i=0;i<cnt;i++)//起点到G、Y之后剩下的能量
    {
        dp[1|(1<<i)][i]=x-d[0][i];

        if(type[i]==1 && dp[1|(1<<i)][i]>=0) dp[1|(1<<i)][i]=x;
    }

    for(i=1;i<(1<<cnt);i++)
    {
        if((i&1)==0) continue;//起点不在集合内

        for(j=0;j<cnt;j++)
        {
            if(dp[i][j]<0) continue;//该状态不能扩展

            if((i&ok)==ok) return 1;

            if(i&(1<<j))//j在集合内
            {
                for(k=1;k<cnt;k++)
                {
                    if((i&(1<<k))==0)//k不在集合内
                    {
                        if(dp[i][j]>=d[j][k])
                        {
                            dp[i|(1<<k)][k]=max(dp[i|(1<<k)][k],dp[i][j]-d[j][k]);

                            if(type[k]==1) dp[i|(1<<k)][k]=x;//如果是能量池
                        }
                    }
                }
            }
        }
    }

    return 0;
}

int main()
{
    int i,j;

    while(~scanf("%d%d",&n,&m) && n)
    {
        for(i=0;i<n;i++) scanf("%s",mp[i]);

        cnt=1;
        ok=0;

        for(i=0;i<n;i++) for(j=0;j<m;j++)
        {
            if(mp[i][j]=='F')
            {
                ok|=1;

                type[0]=0;
                pos[0]=i*20+j;
            }
            else if(mp[i][j]=='G')
            {
                type[cnt]=1;
                pos[cnt]=i*20+j;
                cnt++;
            }
            else if(mp[i][j]=='Y')
            {
                ok|=(1<<cnt);

                type[cnt]=2;
                pos[cnt]=i*20+j;
                cnt++;
            }
        }

        for(i=0;i<cnt;i++)
        {
            bfs(pos[i]/20,pos[i]%20);

            for(j=0;j<cnt;j++) d[i][j]=dis[pos[j]/20][pos[j]%20];
        }

        int l,r,mid,ans;

        l=0;
        ans=r=n*m;

        while(l<=r)
        {
            mid=(l+r)>>1;

            if(check(mid))
            {
                ans=mid;

                r=mid-1;
            }
            else l=mid+1;
        }

        printf("%d\n",ans<n*m?ans:-1);
    }
}
时间: 2024-08-29 05:18:20

HDU-3681-Prison Break(BFS+状压DP+二分)的相关文章

HDU 3681 Prison Break floyd+状压+二分

题目链接:点击打开链接 题意: 给定n*m的矩阵: F:起点(有且仅有一个) D:坏点(不能走到这个点) G:能量池(走到这个点可以选择使用这个点的能量池,把电池充满,也可以暂时不用,只能使用一次) Y:目标点 问: 遍历所有Y点需要最小的电池容量是多少. 开始电池满电,每走一步消耗一格电. Y+G的个数<=15. n,m<=15 思路:状压YG,前面几位表示Y,后面几位表示G. 先跑个floyd,然后二分电池容量,状压dp判可行 #include <cstdio> #includ

HDU 4856 Tunnels(BFS+状压DP)

HDU 4856 Tunnels 题目链接 题意:给定一些管道,然后管道之间走是不用时间的,陆地上有障碍,陆地上走一步花费时间1,求遍历所有管道需要的最短时间,每个管道只能走一次 思路:先BFS预处理出两两管道的距离,然后状态压缩DP求解,dp[s][i]表示状态s,停在管道i时候的最小花费 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using

HDU 3681 BFS&amp;状压DP&amp;二分

N*M矩阵,从F点出发,走完所有的Y点,每走一格花费1点电量,走到G点时,电量充满,D不可到达,问起始时的最小满电量可以走完所有Y,Y和G一共最多15个 先BFS出所有的F,Y,G之间的最短距离. 然后二分起始电量,对每个电量,做状压DP判断是否可行 #include "stdio.h" #include "string.h" #include "queue" using namespace std; int inf=0x3f3f3f3f; in

HDU3681 Prison Break(状压dp)

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

HDU 4114 Disney&#39;s FastPass (状压DP)

题意:给定 n 个区域,然后给定两个区域经过的时间,然后你有 k 个景点,然后给定个每个景点的区域和有票没票的等待时间,从哪些区域能够得到票,问你从景点1开始,最后到景点1,而且要经过看完这k个景点. 析:一个状压DP,dp[s1][s2][i] 表示已经访问了 s1 中的景点,拥有 s2 的票,当前在 i 区域,然后两种转移一种是去下一个景点,另一种是去下一个区域拿票.当时输入,写错了,卡了好长时间.... 代码如下: #pragma comment(linker, "/STACK:10240

POJ 2411 &amp;&amp; HDU 1400 Mondriaan&#39;s Dream (状压dp 经典题)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12341   Accepted: 7204 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

HDU 4906 Our happy ending (状压DP)

HDU 4906 Our happy ending 题目链接 题意:给定n个数字,每个数字可以是0-l,要选其中一些数字,然后使得和为k,问方案 思路:状压dp,滚动数组,状态表示第i个数字,能组成的数字状态为s的状态,然后每次一个数字,循环枚举它要选取1 - min(l,k)的多少,然后进行状态转移 代码: #include <cstdio> #include <cstring> typedef long long ll; const int N = (1<<20)

HDU-3502-Huson&#39;s Adventure Island(BFS+状压DP)

Problem Description A few days ago, Tom was tired of all the PC-games, so he went back to some old FC-games. "Hudson's Adventure Island" was his favorite which he had played thousands of times. But to his disappointed, the more he played, the mo

HDU 3681 Prison Break(bfs+二分+状压DP)

Prison Break Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3778    Accepted Submission(s): 992 Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But on