HDU 3681 Prison Break(bfs+二分+状压DP)

原创 2015年07月06日 19:02:19

Prison Break

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


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
 

/*

题意: D不能走,s可以走,Y是要到达的地方(所以都要经过),G是加油的地方(一个G只能加一次由)
      F是起点,求最少需要多少时间才可以从F经过所有的Y

思路: Y是必到的地方,G是选择性到的,用状压

      1:先bfs求出任意Y,F,G 的最短距离
      2:二分答案,然后用状压DP判断是否可行



*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

using namespace std;

#define INF 0x3f3f3f3f
#define N 16

int dp[1<<N][N];
int all,k;
int dis[N][N][N][N];
int n,m;

struct stud{
  stud(){};
  stud(int xx,int yy):x(xx),y(yy),step(0){};
  int x,y;
  int step;
}f[N];

queue<stud>q;
int step[4][2]={1,0,-1,0,0,1,0,-1};

char c[N][N];
int vis[N][N];
int start;

bool judge(int x,int y)
{
	if(x>=0&&x<n&&y>=0&&y<m)
		return true;
	return false;
}

void bfs(int sx,int sy)
{
   int i,j;
   while(!q.empty()) q.pop();
   memset(vis,0,sizeof(vis));
   stud cur(sx,sy);
   cur.step=0;
   q.push(cur);
   stud next;
   vis[sx][sy]=1;
   while(!q.empty())
   {
   	   cur=q.front();
   	   q.pop();
   	   for(i=0;i<4;i++)
	   {
	   	 int xx=cur.x+step[i][0];
	   	 int yy=cur.y+step[i][1];
	   	 if(!judge(xx,yy)) continue;
	   	 if(c[xx][yy]=='D') continue;
	   	 if(vis[xx][yy]) continue;
	   	 next.x=xx;
	   	 next.y=yy;
	   	 next.step=cur.step+1;
	   	 dis[sx][sy][xx][yy]=next.step;
	   	 vis[xx][yy]=1;
	   	 q.push(next);
	   }
   }
}

inline int getdis(int i,int j)
{
	return dis[f[i].x][f[i].y][f[j].x][f[j].y];
}

bool DP(int mid)
{
    int i,j;
    int len=1<<k,ans=-1;
    memset(dp,-1,sizeof(dp));
    dp[1<<start][start]=mid; //起点

    int cur;
    for(cur=0;cur<len;cur++)
	   for(i=0;i<k;i++)
	   {
	   	  if(!(cur&(1<<i))) continue;
	   	  if((cur&all)==all) ans=max(ans,dp[cur][i]);
	   	  if(dp[cur][i]<=0) continue;

	   	  for(j=0;j<k;j++)
		  {
		  	  if(cur&(1<<j)) continue;
		  	  int next=cur|(1<<j);
		  	  int temp=getdis(i,j);
		  	  if(temp>dp[cur][i]) continue;
		  	  dp[next][j]=max(dp[next][j],dp[cur][i]-temp);
		  	  if(c[f[j].x][f[j].y]=='G')
				dp[next][j]=mid;
		  }
	   }
    return ans>=0;
}

void solve()
{
	int i,j;
	int le=0,ri=250,mid; //二分答案
	int ans=-1;
	while(le<=ri)
	{
		mid=(le+ri)>>1;
		if(DP(mid))
		{
			ans=mid;
			ri=mid-1;
		}
		else
			le=mid+1;
	}
    printf("%d\n",ans);
}

int main()
{
	int i,j;
	while(scanf("%d%d",&n,&m),n+m)
	{
		for(i=0;i<n;i++)
			scanf("%s",c[i]);
		k=0;
		all=0;
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
			   if(c[i][j]=='F')
		         {
			       all|=1<<k;
			       start=k; //起点
			       f[k++]=stud(i,j);
		         }
				else
				  if(c[i][j]=='G')
				{
					f[k++]=stud(i,j);
				}
				else
					if(c[i][j]=='Y')
				{
					all|=1<<k;  //要到达的状态是all
					f[k++]=stud(i,j);
				}
		memset(dis,INF,sizeof(dis));
		for(i=0;i<k;i++)
			bfs(f[i].x,f[i].y);
		solve();
	}
     return 0;
}







版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU3681 Prison Break(壮压dp+二分+bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 题意:一个机器人想越狱,他只能带一定电量的电池,'S'表示道路可行,'G'表示充电器(充满),只...

HDU3681 Prison Break(状压dp)

Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one d...

HDU 3681 Prison Break (搜索+状压dp)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3681 题意: 一个机器人想越狱,他只能带一定电量的电池,'S'表示道路可行,'G'表示充电器...

HDU 3681 Prison Break - 状压dp【TSP】

题目描述题目大意:一个机器人想越狱,监狱是一个N*M的网格。每走一格,耗费1个单位的电量。他只能带一定电量的电池, ‘F’表示起点, ‘S’表示道路可行, ‘D’表示不能经过的地点。 ‘G’表示充电器...

HDU-3681 Prison Break (bfs + 二分枚举)

题目大意:要求主角打开所有开关从而能打开监狱从而逃走。 要求:主角梅走一步都会消耗一单位的能量,不能通过'D',‘G’是补充能量的地方(意味着在此处主角可将能量补到电池容量大小),可以补满能量,...

HDU3681 Prison Break【状压】

题意:给一张图,机器人从F出发,关闭所有Y,问它的最小电池储量,图中G可以把电池充满 思路:把图中的F和G、Y拿出来用作状压DP的点,点之间最短距离跑下bfs。二分答案,用状压DP判断下这个...
  • wjw1340
  • wjw1340
  • 2017年08月09日 16:39
  • 53

HDU3681Prison Break(状态压缩+BFS)

Prison Break Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T...

hdu3681 Prison Break (dfs+二分+状态压缩)

题意:一个机器人一开始在F,D表示障碍,Y表示开关,G表示充电器。 机器人每走一步消耗一点能量,走到充电器可以瞬间充满电(每个充电器只能充一次),现在要打开所有的开关(每个开关也只用打开一次),问最...

hdu 3681 Prison Break

Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one d...

HDU - 3681 Prison Break(状态压缩 + 最短路)

题目大意:有一个机器人想越狱,越狱的要求是将所有的电网开关关掉。现在给出一个地图,’S’表示空地,‘F‘表示起始地点,‘G‘表示充电池,‘D‘表示禁地,‘Y‘开关 充电池可以将机器人的电充满。机器人...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU 3681 Prison Break(bfs+二分+状压DP)
举报原因:
原因补充:

(最多只允许输入30个字)