HDU 3681 Prison Break(状态压缩DP+BFS+二分答案)

Prison Break

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2178    Accepted Submission(s): 533

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  

 

Recommend lcy&zhengfeng       n*m的矩阵,’F’是起点。
机器人从F出发,走到G可以充电,走到Y关掉开关,D不能走进,要求把所有开关关掉,且电量最少,并求出该最小电量。   首先通过BFS求出两点的距离。 可以看出’G’跟’Y’只能去一次,然后使用状态压缩DP进行求解。使用二分求出答案。    

/*
 * HDU 3681
 * 状态压缩DP+BFS+二分答案
 */

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN=17;
struct Node
{
    int x,y;
};
char str[MAXN][MAXN];
int dis[MAXN][MAXN][MAXN][MAXN];//dis[sx][sy][ex][ey]表示从(sx,sy)到(ex,ey)的最短距离,-1表示不能到达
int n,m;

Node node[MAXN];
int cnt;
int start;
int FinalState;

queue<Node>q;
int move[][2]={{1,0},{-1,0},{0,1},{0,-1}};
void BFS(int sx,int sy)//BFS求(sx,sy)出发到其它点的距离
{
    if(str[sx][sy]=='D')return;//这个点的不需要处理
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            dis[sx][sy][i][j]=-1;
    while(!q.empty())q.pop();
    Node tmp;
    tmp.x=sx,tmp.y=sy;
    dis[sx][sy][sx][sy]=0;
    q.push(tmp);
    Node now;
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            now.x=tmp.x+move[i][0];
            now.y=tmp.y+move[i][1];
            if(now.x<0||now.x>=n||now.y<0||now.y>=m)continue;
            if(str[now.x][now.y]=='D')continue;
            if(dis[sx][sy][now.x][now.y]!=-1)continue;
            dis[sx][sy][now.x][now.y]=dis[sx][sy][tmp.x][tmp.y]+1;
            q.push(now);
        }
    }
}

int dp[1<<MAXN][MAXN];

bool check(int v)//判断v体积的能不能符合要求
{
    memset(dp,-1,sizeof(dp));
    dp[1<<start][start]=v;
    for(int i=0;i<(1<<cnt);i++)
        for(int j=0;j<cnt;j++)
        {
            if((i&(1<<j))==0)continue;
            if(dp[i][j]==-1)continue;
            if((i&FinalState)==FinalState)return true;//到达目标状态
            for(int k=0;k<cnt;k++)
            {
                if( i==j || (i&(1<<k) )!=0)continue;
                int tmp=dis[node[j].x][node[j].y][node[k].x][node[k].y];
                if(tmp==-1 || dp[i][j]<tmp)continue;
                dp[i|(1<<k)][k]=max(dp[i|(1<<k)][k],dp[i][j]-tmp);
                if(str[node[k].x][node[k].y]=='G')dp[i|(1<<k)][k]=v;
            }
        }
    return false;
}


int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    while(scanf("%d%d",&n,&m)==2)
    {
        if(n==0 && m==0)break;
        for(int i=0;i<n;i++)scanf("%s",str[i]);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                BFS(i,j);
        cnt=0;
        FinalState=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(str[i][j]=='F')//起点
                {
                    start=cnt;
                    FinalState|=(1<<cnt);
                    node[cnt].x=i;
                    node[cnt].y=j;
                    cnt++;
                }
                else if(str[i][j]=='G')
                {
                    node[cnt].x=i;
                    node[cnt].y=j;
                    cnt++;
                }
                else if(str[i][j]=='Y')
                {
                    FinalState|=(1<<cnt);
                    node[cnt].x=i;
                    node[cnt].y=j;
                    cnt++;
                }
            }
        int l=0,r=n*m;
        int ans=-1;
        while(l<=r)//二分答案,求结果
        {
            int mid=(l+r)/2;
            if(check(mid))
            {
                ans=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        printf("%d\n",ans);

    }
    return 0;
}

 

         

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2013/04/25/3043750.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞