【二分】——hdu3381——bfs预处理;状压DP做判断(★★★)

                                           Prison Break

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


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
 
题意:给出矩阵(作为监狱)和在监狱中的一个装有电池的机器人,其中
  • F为出发点,图中只有一个,且初始状态下机器人在此处电池为满电量;
  • D为障碍物,不可走;
  • S为空格子,机器可自由通过;
  • G为充电点,只能充电一次,且一次能充满电池,经过G可作为S不充电,充电后G变为S;
  • Y点为电闸,机器人可通过,且通过后变为S。

机器人通过所有Y后才能飞出监狱够逃走。

在此之前他只能一个格子一个格子(向上、向下、向左、向右)地走动,而且每走一格消耗一格电池的电量,

如果电池没电他就不能移动。

问他的电池满电量状态下至少有几格电量才能使他逃出监狱?

 

思路:

这道题拿到以后完全没看出来和状压DP有毛线关系

看了别人代码后才理解

分析如下——

要求最小能量,那么最好每个点要走只走一次

那么最大就是225

这就涉及到二分法的大招

将求最优问题转化为决策问题

那这道题不就正好是——旅行商问题的优化与决策函数 吗?

 

这时再注意到15的数据范围,

基本就锁定了TSP问题的状压DP解法

就是需要预处理出来一个最短两点间距

 

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

struct Node{
    int x,y;
}node[17*17];

int dist[17][17][17][17];//dist[x1][y1][x2][y2]表示(x1,y1)到(x2,y2)最短距离
int dp[1<<17][17];//同TSP dp数组
int n,m;
int state,final_state;
int start;//起点位置
char map[17][17];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

void bfs(Node &node)
{
    queue<pair<int,int> >que;
    que.push(make_pair(node.x,node.y));
    dist[node.x][node.y][node.x][node.y]=0;

    while(!que.empty())
    {
        int x=que.front().first;
        int y=que.front().second;
        que.pop();

        for(int i=0;i<4;i++)
        {
            int xx=x+dir[i][0];
            int yy=y+dir[i][1];
            if(xx>=0 && xx<n && yy>=0 && yy<m  &&  map[xx][yy]!='D' )
            {
                if(dist[node.x][node.y][xx][yy]==-1)
                {
                    dist[node.x][node.y][xx][yy]=dist[node.x][node.y][x][y]+1;
                    que.push(make_pair(xx,yy));
                }
            }
        }
    }
}

//判断当前能量是否可行
bool Judge(int Power)
{
    memset(dp,-1,sizeof(dp));

    dp[(1<<start)][start]=Power;

    int res=-1;
    for(int i=0;i<(1<<state);i++)//枚举可操作点的状态
    {
        for(int j=0;j<state;j++)//枚举每个可操作点的位置
        {
            if( ( i & ( 1<<j ) ) ==0)continue;//若状态 i 中不包含 j 点
            if(dp[i][j]<0)continue;//若以 j 为终点的 i 状态不可达

            if( ( i & final_state ) ==final_state )//最终状态是 i 状态的一个子状态
                res=max(res,dp[i][j]);

            for(int k=0;k<state;k++)
            {
                if((i&(1<<k))||(j==k))continue;// i 中不存在 k点 或  j,k 重叠
                if(dist[node[j].x][node[j].y][node[k].x][node[k].y] < 0)continue;// j,k 不连通
                if(dp[i][j] < dist[node[j].x][node[j].y][node[k].x][node[k].y] )continue;//非更优解

                //根据最大剩余能量 选择是否经由 j 点
                dp[i|(1<<k)][k]=max(dp[i|(1<<k)][k] , dp[i][j]-dist[node[j].x][node[j].y][node[k].x][node[k].y]);

                if(map[node[k].x][node[k].y]=='G')
                    dp[i|(1<<k)][k]=Power;
            }
        }
    }
    return res>=0;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)break;

        state=0;//可操作点的个数
        final_state=0;//记录开关位置

        for(int i=0;i<n;i++)
        {
            scanf("%s",map[i]);//输入技巧
            for(int j=0;j<m;j++)
            {
                if(map[i][j]=='F')
                {
                    start=state;
                    node[state].x=i;
                    node[state].y=j;
                    final_state|=(1<<state);
                    state++;
                }
                else if(map[i][j]=='G')
                {
                    node[state].x=i;
                    node[state++].y=j;
                }
                else if(map[i][j]=='Y')
                {
                    node[state].x=i;
                    node[state].y=j;
                    final_state|=(1<<state);
                    state++;
                }
            }
        }

        memset(dist,-1,sizeof(dist));//初始化:全不通

        for(int i=0;i<state;i++)
            bfs(node[i]);

        int low=0,high=230,ans=-1;
        while(low<=high)
        {
            int mid=(low+high)>>1;
            if(Judge(mid))
            {
                ans=mid;
                high=mid-1;
            }
            else
            {
                low=mid+1;
            }
        }

        printf("%d\n",ans);

    }
    return 0;
}

 

posted @ 2016-08-26 15:53  琥珀川||雨露晨曦  阅读(90)  评论(0)    收藏  举报