hdu 3681 * Break (旅行商有关问题)

hdu 3681 * Break (旅行商问题)

* Break

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


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

题意:
一个机器人想越狱,他只能带一定电量的电池,'S'表示道路可行,'G'表示充电器,只可充电一次,但是可以经过很多次。'F'表示起点,'Y'表示要破坏的机关,只需破坏一次,可以经过无数次。'D'表示不能经过的地点。求他能破坏所有机关,带的最小初始电量。

思路:
将'G'、'Y'、'F'这些点抽象出来,bfs预处理两点之间的距离,二分初始电量,用状压dp检验是否能行就够了。

感想:
这题是在搜索题集上找的,开始一直想用bfs或者dfs做出来,bfs WA了,有些状态转移处理不了,dfs TLE了。恩,还是状压dp厉害一点。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
//#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 20
#define maxx 32770
#define mod 1000000000
#define INF 0x3f3f3f3f
using namespace std;

int n,m,ans,cnt,ma;
int sx,sy,est;
int dp[maxx][17];
int id[maxn][maxn],dist[maxn][maxn];
bool vis[maxn][maxn],vv[maxn];
char mp[maxn][maxn];
char s[maxn];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
struct node
{
    int x,y,step;
}cur,now;
queue<node>q;

void bfs(int bx,int by,int k)
{
    int i,j,nx,ny,tx,ty;
    memset(vis,0,sizeof(vis));
    while(!q.empty()) q.pop();
    cur.x=bx;
    cur.y=by;
    cur.step=0;
    vis[bx][by]=1;
    q.push(cur);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        nx=now.x;
        ny=now.y;
        if(mp[nx][ny]=='G'||mp[nx][ny]=='Y'||mp[nx][ny]=='F') dist[k][id[nx][ny]]=now.step;
        for(i=0;i<4;i++)
        {
            tx=nx+dx[i];
            ty=ny+dy[i];
            if(tx<1||tx>n||ty<1||ty>m||mp[tx][ty]=='D'||vis[tx][ty]) continue ;
            cur.x=tx;
            cur.y=ty;
            cur.step=now.step+1;
            vis[tx][ty]=1;
            q.push(cur);
        }
    }
}
void presolve()
{
    int i,j,t;
    est=0;
    cnt=t=-1;
    memset(dist,0x3f,sizeof(dist));
    memset(vv,0,sizeof(vv));
    for(i=1;i<=n;i++)  // 编号 bfs预处理
    {
        for(j=1;j<=m;j++)
        {
            if(mp[i][j]=='G'||mp[i][j]=='Y')
            {
                id[i][j]=++cnt;
                if(mp[i][j]=='Y')
                {
                    est|=(1<<cnt);
                    vv[cnt]=1;
                }
            }
        }
    }
    cnt++;
    vv[cnt]=1;
    id[sx][sy]=cnt;
    bfs(sx,sy,cnt);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            if(mp[i][j]=='G'||mp[i][j]=='Y') bfs(i,j,++t);
        }
    }
    ma=1<<cnt;
}
bool isok(int power)
{
    int i,j,k,st;
    dp[0][cnt]=power;
    for(i=0;i<ma;i++)
    {
        for(j=0;j<=cnt;j++)
        {
            if(dp[i][j]==-1) continue ;
            if((i&est)==est) return true ;
            for(k=0;k<=cnt;k++)
            {
                if(k==j||(i&(1<<k))||dist[k][j]>dp[i][j]) continue ;
                st=i|(1<<k);
                if(vv[k]) dp[st][k]=max(dp[st][k],dp[i][j]-dist[k][j]);
                else dp[st][k]=power;
            }
        }
    }
    return false ;
}
void solve()
{
    int i,j,le,ri,mid;
    le=0;
    ri=1000;
    while(le<ri)
    {
        mid=(le+ri)>>1;
        memset(dp,-1,sizeof(dp));
        if(isok(mid)) ri=mid;
        else le=mid+1;
    }
    if(le==1000) ans=-1;
    else ans=le;
}
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m),n||m)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%s",s);
            for(j=1;j<=m;j++)
            {
                mp[i][j]=s[j-1];
                if(mp[i][j]=='F') sx=i,sy=j;
            }
        }
        presolve();
        solve();
        printf("%d\n",ans);
    }
    return 0;
}