您的位置:首页 > 其它

hdu 3681 Prison Break(状压dp+二分)

2016-10-15 21:36 393 查看


题目链接


Prison Break

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4708    Accepted Submission(s): 1283


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

2010 Asia Hangzhou Regional Contest

题意:

有一个n*m的矩阵,'F'是起点。机器人从F出发,走到G可以充满电,走到Y关掉开关,D是障碍,不能走进,要求把所有开关关掉,且初始电量,并求出该最小电量。(初始时满电)。如果不能关掉所有开关则输出-1.

题解:

首先通过BFS求出任意两点的最短距离。

然后二分答案,使用状态压缩DP进行判断。

参考博客

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int MAXN=20;
int nx[]={1,0,-1,0},ny[]={0,1,0,-1};
char s[MAXN][MAXN];
int dp[1<<17][MAXN],d[MAXN][MAXN][MAXN][MAXN],vis[MAXN][MAXN];
struct node
{
int x,y;
node(int i=0,int j=0):x(i),y(j) {}
}a[MAXN*MAXN];
int n,m;
int F,cnt,S;

void bfs(int i,int j)
{
d[i][j][i][j]=0;
memset(vis,0,sizeof(vis));
queue<node> q;
vis[i][j]=1;
q.push(node(i,j));
while(!q.empty())
{
node cur=q.front();
q.pop();
for(int k=0;k<4;k++)
{
int x=cur.x+nx[k],y=cur.y+ny[k];
if(x<1||x>n||y<1||y>m) continue;
if(s[x][y]=='D') continue;
if(vis[x][y]) continue;
vis[x][y]=1;
d[i][j][x][y]=d[i][j][cur.x][cur.y]+1;
q.push(node(x,y));
}
}
}
bool check(int x)
{
memset(dp,-1,sizeof(dp));
dp[1<<S][S]=x;
for(int i=0;i<(1<<cnt);i++)
{
for(int j=0;j<cnt;j++)
{
if(dp[i][j]==-1) continue;
if((i&F)==F) return true;
if(i&(1<<j))
{
for(int k=0;k<cnt;k++)
{
if((i&(1<<k))==0)
{
int tmp=d[a[j].x][a[j].y][a[k].x][a[k].y];
if(tmp==-1) continue;
if(dp[i][j]<tmp) continue;
else if(s[a[k].x][a[k].y]=='G')
dp[i|(1<<k)][k]=x;
else dp[i|(1<<k)][k]=max(dp[i|(1<<k)][k],dp[i][j]-tmp);
}
}
}
}
}
return false;
}

int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0) break;
F=0,cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%s",s[i]+1);
for(int j=1;j<=m;j++)
{
if(s[i][j]=='G')
a[cnt++]=node(i,j);
else if(s[i][j]=='F')
a[cnt]=node(i,j),F|=(1<<cnt),S=cnt++;
else if(s[i][j]=='Y')
a[cnt]=node(i,j),F|=(1<<cnt),cnt++;
}
}
memset(d,-1,sizeof(d));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(s[i][j]!='D') bfs(i,j);
int l=0,r=n*m,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: