HDU 3681 Prison Break 二分+搜索 The 35th ACM-ICPC Asia Regional Contest (Hangzhou)

 

Prison Break

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


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
The 35th ACM-ICPC Asia Regional Contest (Hangzhou)

Recommend
lcy&zhengfeng
 
 
去年杭州现场赛的题,昨天拿来做队内练习赛TLE+WA无数次。。。
其实就是一个二分+BFS,速度比较慢,网上的解题报告基本都是状态DP。
我的解法:
先用一个DFS判断是否连通。
然后用一个BFS初始化二分的上限。
最后才二分,BFS判断。
其实直接二分BFS就可以了,但是要慢些。
我把状态定义成4维的包含了energy,看了别人的代码才知道3维就够了。
不能用STL要超时啊。。。
 
代码:
//2043 power721 HDU 3681 Accepted 17252 KB 515 ms G++ 3070 B 2011-09-09 10:27:34 
#include<cstdio>
#include<cstring>
#include<queue>
#define N 3000005
using namespace std;

int n,m,cnt,cnt1,num,sx,sy,state;
int f[15][15][13785],hash[15][15],dir[4][2]={0,1,0,-1,1,0,-1,0};
char map[16][16];
bool ff[15][15];
void dfs(int x,int y)
{
	int i,xx,yy;
	ff[x][y]=1;
	if(map[x][y]=='Y')
		num++;
	for(i=0;i<4;i++)
	{
		xx=x+dir[i][0];
		yy=y+dir[i][1];
		if(xx>=0&&yy>=0&&xx<n&&yy<m&&map[xx][yy]!='D'&&!ff[xx][yy])
			dfs(xx,yy);
	}
}
struct node
{
	int x,y,e,s,n;
	node(){}
	node(int xx,int yy,int ee,int ss,int nn=0){x=xx;y=yy;e=ee;s=ss;n=nn;}
}t,ad,q[N];
int bfs()
{
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			for(int k=0;k<1<<cnt;k++)
				f[i][j][k]=0;
	int i,xx,yy,s,nn,head=0,tail=0;
	t=node(sx,sy,0,0,0);
	q[++tail]=t;
	f[sx][sy][0]=0;
	while(head!=tail)
	{
		t=q[head=(head+1)%N];
		for(i=0;i<4;i++)
		{
			s=t.s;
			nn=t.n;
			xx=t.x+dir[i][0];
			yy=t.y+dir[i][1];
			if(xx>=0&&yy>=0&&xx<n&&yy<m&&map[xx][yy]!='D')
			{
				if(map[xx][yy]=='Y')
				{
					s|=1<<hash[xx][yy];
					nn=max(nn,t.e+1);
				}
				if((s&state)==state)
					return nn;
				if(!f[xx][yy][s])
				{
					f[xx][yy][s]=t.e+1;
					ad=node(xx,yy,t.e+1,s);
					q[tail=(tail+1)%N]=ad;
				}
				if(map[xx][yy]=='G'&&(s&1<<hash[xx][yy])==0)
				{
					s|=1<<hash[xx][yy];
					if(!f[xx][yy][s])
					{
						f[xx][yy][s]=0;
						ad=node(xx,yy,0,s);
						q[tail=(tail+1)%N]=ad;
					}
				}
			}
		}
	}
	return 0;
}
int bfs(int mid)
{
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			for(int k=0;k<1<<cnt;k++)
				f[i][j][k]=0;
	int i,xx,yy,s,head=0,tail=0;
	t=node(sx,sy,mid,0);
	q[++tail]=t;
	f[sx][sy][0]=mid;
	while(head!=tail)
	{
		t=q[head=(head+1)%N];
		for(i=0;i<4;i++)
		{
			s=t.s;
			xx=t.x+dir[i][0];
			yy=t.y+dir[i][1];
			if(xx>=0&&yy>=0&&xx<n&&yy<m&&map[xx][yy]!='D')
			{
				if(map[xx][yy]=='Y')
					s|=1<<hash[xx][yy];
				if((s&state)==state)
					return 1;
				if(f[xx][yy][s]<t.e-1)
				{
					f[xx][yy][s]=t.e-1;
					ad=node(xx,yy,t.e-1,s);
					q[tail=(tail+1)%N]=ad;
				}
				if(map[xx][yy]=='G'&&(s&1<<hash[xx][yy])==0)
				{
					s|=1<<hash[xx][yy];
					if(f[xx][yy][s]<mid)
					{
						f[xx][yy][s]=mid;
						ad=node(xx,yy,mid,s);
						q[tail=(tail+1)%N]=ad;
					}
				}
			}
		}
	}
	return 0;
}
int main()
{
	while(scanf("%d%d",&n,&m),n||m)
	{
		int i,j;
		state=cnt=cnt1=num=0;
		for(i=0;i<n;i++)
		{
			scanf("%s",map[i]);
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='Y')
				{
					state|=1<<cnt;
					hash[i][j]=cnt++;
					cnt1++;
				}
				if(map[i][j]=='G')
					hash[i][j]=cnt++;
				if(map[i][j]=='F')
				{
					sx=i;
					sy=j;
				}
			}
		}
		if(!state)
		{
			puts("0");
			continue;
		}
		memset(ff,0,sizeof(ff));
		dfs(sx,sy);
		if(num!=cnt1)
		{
			puts("-1");
			continue;
		}
		int l=0,r=bfs(),mid,ans=-1;
		while(l<=r)
		{
			mid=(l+r)/2;
			if(bfs(mid))
			{
				ans=mid;
				r=mid-1;
			}
			else
				l=mid+1;
		}
		printf("%d\n",ans);
	}
}


简单代码:
//2030 power721 HDU 3681 Accepted 18792 KB 1515 ms C++ 1738 B 2011-09-09 10:05:26 
#include<cstdio>
#include<cstring>
#include<queue>
#define N 3000005
using namespace std;

int n,m,cnt,sx,sy,state;
int f[15][15][13785],hash[15][15],dir[4][2]={0,1,0,-1,1,0,-1,0};
char map[25][25];
struct node
{
	int x,y,e,s;
	node(){}
	node(int xx,int yy,int ee,int ss){x=xx;y=yy;e=ee;s=ss;}
}t,ad,q[N];
int bfs(int mid)
{
	memset(f,0,sizeof(f));
	int i,xx,yy,s,head=0,tail=0;
	t=node(sx,sy,mid,0);
	q[++tail]=t;
	f[sx][sy][0]=mid;
	while(head!=tail)
	{
		t=q[head=(head+1)%N];
		for(i=0;i<4;i++)
		{
			s=t.s;
			xx=t.x+dir[i][0];
			yy=t.y+dir[i][1];
			if(xx>=0&&yy>=0&&xx<n&&yy<m&&map[xx][yy]!='D')
			{
				if(map[xx][yy]=='Y')
					s|=1<<hash[xx][yy];
				if((s&state)==state)
					return 1;
				if(f[xx][yy][s]<t.e-1)
				{
					f[xx][yy][s]=t.e-1;
					ad=node(xx,yy,t.e-1,s);
					q[tail=(tail+1)%N]=ad;
				}
				if(map[xx][yy]=='G'&&(s&1<<hash[xx][yy])==0)
				{
					s|=1<<hash[xx][yy];
					if(f[xx][yy][s]<mid)
					{
						f[xx][yy][s]=mid;
						ad=node(xx,yy,mid,s);
						q[tail=(tail+1)%N]=ad;
					}
				}
			}
		}
	}
	return 0;
}
int main()
{
	while(scanf("%d%d",&n,&m),n||m)
	{
		int i,j;
		state=cnt=0;
		memset(hash,-1,sizeof(hash));
		for(i=0;i<n;i++)
		{
			scanf("%s",map[i]);
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='Y')
				{
					state|=1<<cnt;
					hash[i][j]=cnt++;
				}
				if(map[i][j]=='G')
					hash[i][j]=cnt++;
				if(map[i][j]=='F')
				{
					sx=i;
					sy=j;
				}
			}
		}
		if(!state)
		{
			puts("0");
			continue;
		}
		int l=0,r=n*m,mid,ans=-1;
		while(l<=r)
		{
			mid=(l+r)/2;
			if(bfs(mid))
			{
				ans=mid;
				r=mid-1;
			}
			else
				l=mid+1;
		}
		printf("%d\n",ans);
	}
}

你可能感兴趣的:(struct,Integer,ini,each,output,Numbers)