关闭

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

标签: bfsdp
427人阅读 评论(1) 收藏 举报
分类:

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;
}







1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:178225次
    • 积分:5664
    • 等级:
    • 排名:第4583名
    • 原创:389篇
    • 转载:9篇
    • 译文:0篇
    • 评论:14条
    最新评论