Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2178 Accepted Submission(s): 533
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.
/* * HDU 3681 * 状态压缩DP+BFS+二分答案 */ #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> using namespace std; const int MAXN=17; struct Node { int x,y; }; char str[MAXN][MAXN]; int dis[MAXN][MAXN][MAXN][MAXN];//dis[sx][sy][ex][ey]表示从(sx,sy)到(ex,ey)的最短距离,-1表示不能到达 int n,m; Node node[MAXN]; int cnt; int start; int FinalState; queue<Node>q; int move[][2]={{1,0},{-1,0},{0,1},{0,-1}}; void BFS(int sx,int sy)//BFS求(sx,sy)出发到其它点的距离 { if(str[sx][sy]=='D')return;//这个点的不需要处理 for(int i=0;i<n;i++) for(int j=0;j<m;j++) dis[sx][sy][i][j]=-1; while(!q.empty())q.pop(); Node tmp; tmp.x=sx,tmp.y=sy; dis[sx][sy][sx][sy]=0; q.push(tmp); Node now; while(!q.empty()) { tmp=q.front(); q.pop(); for(int i=0;i<4;i++) { now.x=tmp.x+move[i][0]; now.y=tmp.y+move[i][1]; if(now.x<0||now.x>=n||now.y<0||now.y>=m)continue; if(str[now.x][now.y]=='D')continue; if(dis[sx][sy][now.x][now.y]!=-1)continue; dis[sx][sy][now.x][now.y]=dis[sx][sy][tmp.x][tmp.y]+1; q.push(now); } } } int dp[1<<MAXN][MAXN]; bool check(int v)//判断v体积的能不能符合要求 { memset(dp,-1,sizeof(dp)); dp[1<<start][start]=v; for(int i=0;i<(1<<cnt);i++) for(int j=0;j<cnt;j++) { if((i&(1<<j))==0)continue; if(dp[i][j]==-1)continue; if((i&FinalState)==FinalState)return true;//到达目标状态 for(int k=0;k<cnt;k++) { if( i==j || (i&(1<<k) )!=0)continue; int tmp=dis[node[j].x][node[j].y][node[k].x][node[k].y]; if(tmp==-1 || dp[i][j]<tmp)continue; dp[i|(1<<k)][k]=max(dp[i|(1<<k)][k],dp[i][j]-tmp); if(str[node[k].x][node[k].y]=='G')dp[i|(1<<k)][k]=v; } } return false; } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); while(scanf("%d%d",&n,&m)==2) { if(n==0 && m==0)break; for(int i=0;i<n;i++)scanf("%s",str[i]); for(int i=0;i<n;i++) for(int j=0;j<m;j++) BFS(i,j); cnt=0; FinalState=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(str[i][j]=='F')//起点 { start=cnt; FinalState|=(1<<cnt); node[cnt].x=i; node[cnt].y=j; cnt++; } else if(str[i][j]=='G') { node[cnt].x=i; node[cnt].y=j; cnt++; } else if(str[i][j]=='Y') { FinalState|=(1<<cnt); node[cnt].x=i; node[cnt].y=j; cnt++; } } int l=0,r=n*m; int ans=-1; while(l<=r)//二分答案,求结果 { int mid=(l+r)/2; if(check(mid)) { ans=mid; r=mid-1; } else l=mid+1; } printf("%d\n",ans); } return 0; }