HDU3681 Prison Break

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


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
 

 

Recommend
lcy&zhengfeng

 

动态规划 状压DP 二分

停在普通点的状态肯定不需要保留,只记录每个特殊点(起点 开关 能量点)的位置,预处理出它们之间的距离。

压缩状态,f[bit][ter]表示当前到达过的点集为bit,停留在ter号点的最大剩余能量。

假定所有的点都只能停留一次(预处理时按可以多次经过来计算距离,而表示状态时多停留并没有用)

二分答案,判定是否可行

 

顺带一提,由于这份代码中用了大量STL,并且HDU貌似没有-O2,耗时成功垫底233

题目不算难,但状压dp细节好麻烦,位运算时候总是一眼花就写错变量,看好半天才能发现,就很气。

  1 /*by SilverN*/
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<cmath>
  7 #include<vector>
  8 #include<queue>
  9 using namespace std;
 10 const int mxn=100010;
 11 const int mx[5]={0,0,1,0,-1};
 12 const int my[5]={0,1,0,-1,0};
 13 int f[16][1<<16];
 14 char mp[16][16];
 15 int n,m;
 16 int sx,sy;
 17 int yx[16],yy[16],yct=0;
 18 int b[16],bct=0;int tar;
 19 int dis[16][16][16][16];
 20 bool vis[16][16];
 21 void init(){
 22     memset(dis,0x3f,sizeof dis);
 23     memset(f,-1,sizeof f);
 24     return;
 25 }
 26 void BFS(int sx,int sy){
 27     dis[sx][sy][sx][sy]=0;
 28     memset(vis,0,sizeof vis);
 29     queue<pair<int,int> >q;
 30     q.push(make_pair(sx,sy));
 31     vis[sx][sy]=1;
 32     while(!q.empty()){
 33         int x=q.front().first,y=q.front().second;q.pop();
 34         for(int i=1;i<=4;i++){
 35             int nx=x+mx[i],ny=y+my[i];
 36             if(nx<1 || nx>n || ny<1 || ny>m)continue;
 37             if(vis[nx][ny] || mp[nx][ny]=='D')continue;
 38             vis[nx][ny]=1;
 39             dis[sx][sy][nx][ny]=dis[sx][sy][x][y]+1;
 40             q.push(make_pair(nx,ny));
 41         }
 42     }
 43     return;
 44 }
 45 bool solve(int lim){
 46     memset(f,-1,sizeof f);
 47     f[0][1]=lim;
 48     int i,j,k,ed=(1<<(yct+1))-1;
 49     for(i=0;i<=ed;i++){
 50         for(j=0;j<=yct;j++){
 51             if(!(i&b[j]))continue;
 52             if((i&tar)==tar && f[j][i]!=-1)return 1;
 53             if(f[j][i]==-1)continue;
 54             for(k=0;k<=yct;k++){
 55                 if(j==k || (i&b[k]))continue;
 56                 int dist=f[j][i]-dis[yx[j]][yy[j]][yx[k]][yy[k]];
 57                 if(dist<0)continue;
 58                 f[k][i|b[k]]=max(f[k][i|b[k]],dist);
 59                 if(mp[yx[k]][yy[k]]=='G'){f[k][i|b[k]]=lim;}
 60             }
 61         }
 62     }
 63     return 0;
 64 }
 65 int main(){
 66     int i,j;
 67     while(scanf("%d%d",&n,&m) && n && m){
 68         init();bct=yct=0;
 69         tar=0;
 70         for(i=1;i<=n;i++)scanf("%s",mp[i]+1);
 71         for(i=1;i<=n;i++)
 72             for(j=1;j<=m;j++){
 73                 if(mp[i][j]=='F'){yx[0]=i,yy[0]=j;}
 74                 else if(mp[i][j]=='Y'){yx[++yct]=i;yy[yct]=j;b[yct]=1<<(++bct);tar|=b[yct];}
 75                 else if(mp[i][j]=='G'){yx[++yct]=i;yy[yct]=j;b[yct]=1<<(++bct);}
 76             }
 77         b[0]=1;
 78 /*        for(i=0;i<=yct;i++){
 79             printf("%d %d",yx[i],yy[i]);
 80             printf("  type:%c\n",mp[yx[i]][yy[i]]);
 81             printf("b:%d\n",b[i]);
 82         }
 83         printf("tat:%d\n",tar);*/
 84         for(i=0;i<=yct;i++)BFS(yx[i],yy[i]);
 85 /*        for(i=0;i<=yct;i++)
 86             for(j=0;j<=yct;j++){
 87                 printf("(%d %d) to (%d %d):",yx[i],yy[i],yx[j],yy[j]);
 88                 printf("%d\n",dis[yx[i]][yy[i]][yx[j]][yy[j]]);
 89             }
 90 */        
 91         int l=0,r=n*m*5,ans=1e8;
 92         while(l<=r){
 93             int mid=(l+r)>>1;
 94             if(solve(mid)){
 95                 ans=mid;
 96                 r=mid-1;
 97             }
 98             else l=mid+1;
 99         }
100         if(ans<=n*m*5)printf("%d\n",ans);
101         else printf("-1\n");
102     }
103     return 0;
104 }

 

posted @ 2017-02-22 15:35  SilverNebula  阅读(341)  评论(0编辑  收藏  举报
AmazingCounters.com