HDU-1813 Escape from Tetris(IDA*)

题意

http://acm.hdu.edu.cn/showproblem.php?pid=1813

思路

压缩状态是 A* \text{A*} A* 算法中重要的一部分,例如这道题,我们要求任意一个点都能走出去,那不如现在所有点上都放上一个小人,随着上下左右的操作让他们走。一个格子只有“有小人”、“没小人”两种状态,就可以用二进制数表示。而格子最多只有 ( 8 − 2 ) 2 = 36 (8-2)^2=36 (82)2=36 个,一个 long long \text{long long} long long 足矣存储。小人的行走也直接体现在二进制的位运算上。
这题的启发函数也比较显然,通过把边缘一圈点推进队列里进行广搜,预处理出每个点出去的最少步数,一个状态的最优情况就是所有的小人走出去的最少步数之 max ⁡ \max max

代码

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#define FOR(i,x,y) for(int i=(x),i##END=(y);i<=i##END;++i)
#define DOR(i,x,y) for(int i=(x),i##END=(y);i>=i##END;--i)
typedef long long LL;
using namespace std;
const int dx[4]={0,-1,1,0};
const int dy[4]={1,0,0,-1};
const char dirc[4][10]={"east","north","south","west"};
const int N=12;
bool mp[N][N];
int dis[N][N],n;
bool vis[N][N];
int stk[100003];
char str[N];
struct node{int x,y,stp;};
int hs(int x,int y){return (x-2)*(n-2)+y-2;}
void PreCompute()
{
	memset(vis,0,sizeof(vis));
	queue<node>Q;
	while(!Q.empty())Q.pop();
	FOR(i,2,n-1)
	{
		if(!mp[i][1])Q.push((node){i,1,0});
		if(!mp[i][n])Q.push((node){i,n,0});
		vis[i][1]=1,vis[i][n]=1;
	}
	FOR(j,2,n-1)
	{
		if(!mp[1][j])Q.push((node){1,j,0});
		if(!mp[n][j])Q.push((node){n,j,0});
		vis[1][j]=1,vis[n][j]=1;
	}
	vis[1][1]=vis[1][n]=vis[n][1]=vis[n][n]=1;
	FOR(i,0,n+1)vis[0][i]=vis[i][0]=vis[n+1][i]=vis[i][n+1]=1;
	while(!Q.empty())
	{
		node now=Q.front();Q.pop();
		FOR(i,0,3)
		{
			int fx=now.x+dx[i],fy=now.y+dy[i],fstp=now.stp+1;
			if(!mp[fx][fy]&&!vis[fx][fy])
			{
				dis[fx][fy]=fstp;
				Q.push((node){fx,fy,fstp}),vis[fx][fy]=1;
			}
		}
	}
}
int H(LL B)
{
	int maxer=0;
	FOR(i,2,n-1)FOR(j,2,n-1)if(B&(1ll<<hs(i,j)))maxer=max(maxer,dis[i][j]);
	return maxer;
}
void Print(int len)
{
	FOR(i,0,len-1)puts(dirc[stk[i]]);
}
bool IDAstar(int rest,LL B,int len)
{
	if(H(B)>rest)return false;
	if(rest==0){Print(len);return true;}
	LL _B;
	
	_B=B;
	FOR(i,2,n-1)DOR(j,n-1,2)if(_B&(1ll<<hs(i,j)))if(!mp[i][j+1])
	{
		_B^=1ll<<hs(i,j);
		if(j!=n-1)_B|=1ll<<hs(i,j+1);
	}
	stk[len]=0;
	if(B!=_B&&IDAstar(rest-1,_B,len+1))return true;
	
	_B=B;
	FOR(i,2,n-1)FOR(j,2,n-1)if(_B&(1ll<<hs(i,j)))if(!mp[i-1][j])
	{
		_B^=1ll<<hs(i,j);
		if(i!=2)_B|=1ll<<hs(i-1,j);
	}
	stk[len]=1;
	if(B!=_B&&IDAstar(rest-1,_B,len+1))return true;
	
	_B=B;
	DOR(i,n-1,2)FOR(j,2,n-1)if(_B&(1ll<<hs(i,j)))if(!mp[i+1][j])
	{
		_B^=1ll<<hs(i,j);
		if(i!=n-1)_B|=1ll<<hs(i+1,j);
	}
	stk[len]=2;
	if(B!=_B&&IDAstar(rest-1,_B,len+1))return true;
	
	_B=B;
	FOR(i,2,n-1)FOR(j,2,n-1)if(_B&(1ll<<hs(i,j)))if(!mp[i][j-1])
	{
		_B^=1ll<<hs(i,j);
		if(j!=2)_B|=1ll<<hs(i,j-1);
	}
	stk[len]=3;
	if(B!=_B&&IDAstar(rest-1,_B,len+1))return true;
	return false;
}

int main()
{
	bool pr=0;
	while(~scanf("%d",&n))
	{
		memset(mp,0,sizeof(mp));
		FOR(i,1,n)
		{
			scanf("%s",str+1);
			FOR(j,1,n)mp[i][j]=str[j]-'0';
		}
		if(pr)puts("");pr=1;
		PreCompute();
		LL B=0;
		FOR(i,2,n-1)FOR(j,2,n-1)if(!mp[i][j])B|=1ll<<hs(i,j);
		for(int i=0;;i++)if(IDAstar(i,B,0))break;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值