hdu3681 BFS+TSP+二分

Prison Break

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


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
 

我很懒还是看别人的题解吧,传送门:题解 

这题先要提取出G,F,Y这三种点,然后用多次bfs求得这些点两两之间的最短路,千万不要用floyd,会TLE的。

然后问题化归为从F点出发经过所有的Y至少一次,G可经过也可不经过的最少充电量,这题经过点的次数不受限制,因此可以先求出两两之间的最短路,原因可以看我的另一篇博客,传送门:类TSP 

仔细看,你会发现这两个问题实质是一样的,我已经在类TSP讲清楚了,为什么这么处理是对的。我们只关注每个点第一次被经过的时刻,这题因为不需要经过所有点,因此只要 有一个状态包含了所有的Y,并且电量的剩余量大于等于0就可以了。但是这样不好求最小电量,因此我们需要二分答案,将其变成判定性问题,然后举个击破就可以了。

注意一个坑点:二分上界不可以设置成inf,否则TLE。

代码:

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

const int inf=0x3f3f3f3f;
struct point{
    int x,y,d;
    point(){}
    point(int xx,int yy,int dd):x(xx),y(yy),d(dd){}
}p[20];
int tot;
char str[20][20];
point q[300];
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
int dist[20][20];
int vis[20][20],id[20][20];
int n,m;
void bfs(point u){
    int s=0,e=-1;
    u.d=0;
    q[++e]=u;
    memset(vis,0,sizeof vis);
    vis[u.x][u.y]=1;
    while(s<=e){
        point v=q[s++];
        if(id[v.x][v.y]!=-1) dist[id[u.x][u.y]][id[v.x][v.y]]=v.d;
        for(int i=0;i<4;i++){
            int tx=v.x+dx[i],ty=v.y+dy[i];
            if(tx<0||ty<0||tx>=n||ty>=m) continue;
            if(vis[tx][ty]||str[tx][ty]=='D') continue;
            q[++e]=point(tx,ty,v.d+1);
            vis[tx][ty]=1;
        }
    }
}
int dp[1<<15][20];
int sx,sy,ori;
int DP(int x){
    memset(dp,-1,sizeof dp);
    int t=id[sx][sy];
    dp[1<<t][t]=x;
    for(int s=0;s<1<<tot;s++){
        for(int i=0;i<tot;i++){
            if((s|ori)==s&&dp[s][i]!=-1) return true;
            if(!(s&1<<i)) continue;
            for(int j=0;j<tot;j++){
                if(i==j||(s&1<<j)||dp[s][i]<dist[i][j]) continue;
                int ns=s+(1<<j);
                dp[ns][j]=max(dp[ns][j],dp[s][i]-dist[i][j]);
                if(str[p[j].x][p[j].y]=='G') dp[ns][j]=x;
            }
        }
    }
    return false;
}
int main()
{
    while(scanf("%d%d",&n,&m),n){
        for(int i=0;i<n;i++)
            scanf("%s",str[i]);
        tot=ori=0;
        memset(id,-1,sizeof id);
        for(int i=0;i<n;i++) //将F,G,Y抽出来
            for(int j=0;str[i][j];j++)
                if(str[i][j]!='D'&&str[i][j]!='S'){
                    p[tot].x=i,p[tot].y=j;
                    id[i][j]=tot;
                    if(str[i][j]=='Y') ori|=1<<tot;
                    if(str[i][j]=='F') sx=i,sy=j;
                    tot++;
                }
        memset(dist,0x3f,sizeof dist);
        for(int i=0;i<tot;i++) bfs(p[i]);
        int l=0,r=300;
        int k=0;
        while(l<r){
            int mid=l+r>>1;
            if(DP(mid)) r=mid;
            else l=mid+1;
        }
        if(l==300) l=-1;
        printf("%d\n",l);
    }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值