• 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
     

    思路:先用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 ",ans<n*m?ans:-1); } }



  • 相关阅读:
    MySQL优化—工欲善其事,必先利其器(2)
    MySQL优化—工欲善其事,必先利其器之EXPLAIN
    Linux工具安装和常用配置
    .Net Core配置文件介绍
    Centos7上开发.Net Core项目
    VMWare的host-only/bridged/NAT连接图文介绍
    Quartz.net 3.x使用总结(二)——Db持久化和集群
    Vuex实现状态管理
    Quartz.net 3.x使用总结(一)——简单使用
    C#获取根目录的方法总结
  • 原文地址:https://www.cnblogs.com/lytwajue/p/6842613.html
Copyright © 2020-2023  润新知