HDU 3861 Prison Breake 状态压缩dp+BFS+二分答案

题意:机器人有一个初始能量x,每走到G点时可选择充满能量(初始能量是满的),每走一步消耗一点能量,问当x最小为多少时,可以把所有的Y都走一遍,输出最小的x!

注意:G点和Y点加一起最多15个

附ac代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
char map[16][16];
int dp[1<<16][16],dis[16][16];
int tot[16][2];
int cnt,first,n,m,ans;
int mark[16][16];
int dui[4][2]={0,1,0,-1,1,0,-1,0};
int min(int a,int b)
{
	if(a<b)
	return a;
	return b;

}
int max(int a,int b)
{
	if(a>b)
	return a;
	return b;
}
struct node{
	int x,y,time;
}cur,next;
int bfs(int x1,int y1,int x2,int y2)
{
	memset(mark,0,sizeof(mark));
	queue<node>q;
	cur.x=x1;
	cur.y=y1;
	cur.time=0;
	while(!q.empty())
	q.pop();
	q.push(cur);
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			next.x=cur.x+dui[i][0];
			next.y=cur.y+dui[i][1];
			next.time=cur.time+1;
			if(map[next.x][next.y]=='D')
			continue;
			if(next.x>=n||next.y>=m||next.x<0||next.y<0)
			continue;
			if(next.x==x2&&next.y==y2)
			return next.time;
			if(mark[next.x][next.y])
			continue;

			mark[next.x][next.y]=1;
			q.push(next);
		}
	}
	return -1;
}
int fun(int kkk)
{
	memset(dp,-1,sizeof(dp));
	int i,j;
	dp[1<<first][first]=kkk;
	//printf("ans=%d\n",ans);
	for(i=0;i<(1<<cnt);i++)
	{

		for(j=0;j<cnt;j++)
		{
			if(i&(1<<j)==0)continue;
			if((i&ans)==ans&&dp[i][j]!=-1)
			return 1;
			for(int k=0;k<cnt;k++)
			{
				if(k==j)
				continue;
				if(((1<<k)&i))
				continue;
				int tem=dp[i][j]-dis[j][k];
				if(tem<0)
				continue;
				if(dis[j][k]==-1)
				continue;
				if(dp[i][j]==-1)
				continue;
				dp[i+(1<<k)][k]=max(tem,dp[i+(1<<k)][k]);
				if(map[tot[k][0]][tot[k][1]]=='G')
				dp[i+(1<<k)][k]=kkk;
				int l=i+(1<<k);
				if(dp[l][k]!=-1)
				{
					if((l&ans)==ans)
					return 1;
				}
			}
		}
	}
	return 0;
}
int main()
{
	int i,j;

	while(scanf("%d%d",&n,&m)!=EOF)
	{ans=0;cnt=0;
	if(n==0&&m==0)
	break;
		for(i=0;i<n;i++)
		{
			scanf("%s",map[i]);
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='Y')
				{
					tot[cnt][0]=i;
					tot[cnt][1]=j;
					ans+=(1<<cnt);
					cnt++;

				}
				if(map[i][j]=='F')
				{
					first=cnt;
					tot[cnt][0]=i;
					tot[cnt][1]=j;

					ans+=(1<<cnt);
					cnt++;

				}
				if(map[i][j]=='G')
				{
					tot[cnt][0]=i;
					tot[cnt][1]=j;
					cnt++;
				}
			}
		}
		memset(dis,-1,sizeof(dis));
		for(i=0;i<cnt;i++)
		{
			for(j=0;j<cnt;j++)
			{
				if(i==j)
				dis[i][j]=0;
				else
				dis[i][j]=bfs(tot[i][0],tot[i][1],tot[j][0],tot[j][1]);
			}
		}
		int l=1,r=225;
		int aaa=1<<30;
		while(l<=r)
		{
			int mid=(l+r)/2;
			if(fun(mid))
			{
				r=mid-1;
				aaa=min(ans,mid);
			}
			else
			l=mid+1;
		}
		if(aaa==(1<<30))
		printf("-1\n");
		else
		printf("%d\n",aaa);
	}
	return  0;
}
时间: 2024-10-15 22:00:58

HDU 3861 Prison Breake 状态压缩dp+BFS+二分答案的相关文章

hdu 3681 Prison Break(状态压缩+bfs)

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

hdu 3217 Health(状态压缩DP)

Health Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 527    Accepted Submission(s): 145 Problem Description Unfortunately YY gets ill, but he does not want to go to hospital. His girlfriend LM

hdu 4057 AC自动机+状态压缩dp

http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for them. 2012 is coming, and Dr. X wants to take some rabbits to Noah's Ark, or there are no rabbits any more.

hdu 3681 二分+状态压缩dp+bfs

题意就不用多说了,开始想直接bfs+二分简化下做试试   果断超时,然后不得不用状态压缩dp(其实一开始就想这样做的,15个点   每个点都能走多次   绝逼状态压缩dp) 先bfs跑每个关键点之间的做短路径    再二分起点的能量值               状态压缩dp来判断     不说了    AC吧!!!!! #include<stdio.h> #include<string.h> #include<queue> #include<iostream&g

HDU 3001 Travelling(状态压缩DP+三进制)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题目大意:有n个城市,m条路,每条路都有一定的花费,可以从任意城市出发,每个城市不能经过两次以上,要求经过所有城市并且花费最少,求出最小花费. 解题思路:三进制的状态压缩DP,跟二进制还是有一点不一样的,因为三进制没有直接的位运算,还要自己先做处理利用num[i][j]记录数字i各位的三进制表示方便计算,其他的就跟二进制状态压缩没有太大区别了.还有注意: ①开始要将n个起点初始化,dp[bit

HDU 4511 (AC自动机+状态压缩DP)

题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4511 题目大意:从1走到N,中间可以选择性经过某些点,比如1->N,或1->2->N,但是某些段路径(注意不是某些条)是被禁止的.问从1->N的最短距离. 解题思路: AC自动机部分: 如果只是禁掉某些边,最短路算法加提前标记被禁的边即可. 但是本题是禁掉指定的路段,所以得边走边禁,需要一个在线算法. 所以使用AC自动机来压缩路段,如禁掉的路段是1->2->3,那么in

hdu 4778 Gems Fight!(状态压缩DP)

又是一道状态压缩,刚开始老是往博弈的方法想,总是没思路. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N=21; const int inf=0x3f3f3f3f; int g,n,s; int sum[1<<N]; int res[10]; int c[22][10]; i

HDU 1074 Doing Homework(状态压缩DP)

题意:有n门课,每门课有截止时间和完成所需的时间,如果超过规定时间完成,每超过一天就会扣1分,问怎样安排做作业的顺序才能使得所扣的分最小 思路:二进制表示. 1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cstdio> 6 #include<set> 7 #include<map> 8

HDU 1074 Doing Homework(状态压缩 + DP)

Problem Description: Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will