HDU 3681 Prison Break

160 篇文章 0 订阅
48 篇文章 0 订阅
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+二分+最短路~

(我感觉)一道很厉害的题……

一共有5种状态:D不能进,S为空地,F为起点,G充电,必须经过Y。但是实际上对结果有较大影响的只有F和Y。其余的D用于判断连通;S没有用;G标记一下,在DP的时候判断是否能经过并充电即可。

所以寻找F,Y,G,给他们顺序标号,其中F一定要标0,然后因为题目中说到F+Y+G<=15非常像是状态压缩,所以我们对这些点求两两之间的最短路(循环+SPFA),二分结果,再状压DP检测这个结果是否可行。

所以问题的DP就转化为了旅行商问题。假设此时二分出的值mid=u,则f[1][0]=u,然后用小的f更新大的f,如果目前已有的f为初始值-1,表示这个点已经不能走到,就不用再往下更新。枚举下一次走到哪个点j,则f[k^(1<<j)][j]=max(f[k^(1<<j)][j],f[k][i]-dis[i][j])。要注意的是我们还可以给机器人充电,所以如果w[j]==1(即G)并且j可以走到(f[k^(1<<j)][j]>=0),那么就在j点充满电(f[k^(1<<j)][j]=u)再往下更新。如果所有点都已经走到并且还有f[tot][i]不为-1,那么这个u就是可行的,返回1。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
#define inf 999999999

int n,m,dis[16][16],d[16][16],cnt,ch[2][4]={{0,1,0,-1},{1,0,-1,0}},tot,num[16][16],ans,f[32770][16];
bool b[16][16],w[16];
char s[16][16];

int cal(int u1,int v1,int u,int v)
{
	if(u1==u && v1==v) return 0;
	memset(b,0,sizeof(b));
	queue<int> qx,qy;b[u1][v1]=1;d[u1][v1]=0;
	qx.push(u1);qy.push(v1);
	while(!qx.empty())
	{
		int x=qx.front(),y=qy.front();qx.pop();qy.pop();
		for(int i=0;i<4;i++)
		{
			int xx=x+ch[0][i],yy=y+ch[1][i];
			if(xx==0 || yy==0 || xx>n || yy>m || s[xx][yy]=='D') continue;
			if(b[xx][yy]) continue;
			b[xx][yy]=1;
			d[xx][yy]=d[x][y]+1;
			if(xx==u && yy==v) return d[xx][yy];
			qx.push(xx);qy.push(yy);
		}
	}
	return inf;
}

bool findd(int u)
{
	int maxx=1<<(cnt+1);
	memset(f,-1,sizeof(f));f[1][0]=u;
	for(int k=1;k<maxx;k++)
	  for(int i=0;i<=cnt;i++)
	    if(f[k][i]!=-1)
	    {
	    	if((k&tot)==tot) return 1;
	    	for(int j=0;j<=cnt;j++)
	    	  if(!(k&(1<<j)))
	    	  {
	    	  	f[k^(1<<j)][j]=max(f[k^(1<<j)][j],f[k][i]-dis[i][j]);
	    	  	if(f[k^(1<<j)][j]>=0 && w[j]) f[k^(1<<j)][j]=u;
			  }
		}
	return 0;
}

int main()
{
	while(scanf("%d%d",&n,&m)==2 && n)
	{
		memset(num,-1,sizeof(num));
		memset(w,0,sizeof(w));
		for(int i=1;i<=n;i++) scanf("%s",s[i]+1);cnt=0;tot=1;
		for(int i=1;i<=n;i++)
		  for(int j=1;j<=m;j++)
		  {
		  	if(s[i][j]=='D' || s[i][j]=='S') continue;
		  	for(int x=i;x<=n;x++)
		  	  for(int y=(x==i ? j:1);y<=m;y++)
		  	    if(s[x][y]!='D' && s[x][y]!='S')
		  	    {
		  	    	if(num[x][y]==-1)
		  	    	{
		  	    		if(s[x][y]=='F') num[x][y]=0;
		  	    		else num[x][y]=++cnt;
					}
					if(s[x][y]=='G') w[num[x][y]]=1;
					else tot|=(1<<num[x][y]);
					dis[num[x][y]][num[i][j]]=dis[num[i][j]][num[x][y]]=cal(i,j,x,y);
				}
		  }
		int l=0,r=0;ans=inf;
		for(int i=0;i<=cnt;i++)
		  for(int j=i+1;j<=cnt;j++) if(dis[i][j]!=inf) r=max(r,dis[i][j]*cnt);
		while(l<=r)
		{
			int mid=(l+r)>>1;
			if(findd(mid)) r=mid-1,ans=min(ans,mid);
			else l=mid+1;
		}
		if(ans==inf) printf("-1\n");
		else printf("%d\n",ans);
	}
	return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值