HDU-1667 The Rotation Game(IDA*)

题意

https://www.luogu.org/problemnew/show/UVA1343

思路

首先枚举终态是 1 , 2 , 3 1,2,3 1,2,3 中的哪一个,然后再把 24 24 24 个数字压成一个长度为 24 24 24 01 01 01 串( 1 1 1 表示终态的数字),直接用 int \text{int} int 来表示这个数。
可以先打表出每一个操作,转动了哪些位置,然后就可以很快的挪动这个 01 01 01串。
A* \text{A*} A* 最有趣的地方就在于 H H H 函数(估价函数)的设计 (H stands for Heuristics) \text{(H stands for Heuristics)} (H stands for Heuristics),它指的是一个状态达到目标状态的最小可能的步数,其中 H ( n ) ≤ H ′ ( n ) , H ( n ) ≤ c o s t ( n , n ′ ) + H ( n ′ ) H(n)\leq H'(n),H(n)\leq cost(n,n')+ H(n') H(n)H(n),H(n)cost(n,n)+H(n),不难看出 H H H 函数是往优取的。如果 H ( ) > r e s t H()>rest H()>rest 就是说即使最优也不够走,那么可以进行剪枝。
对于这道题,我们不难发现,如果有 n n n 个数不在它该在的位置上,那么至少要移动 n n n 遍才能完成,那么我们可以写出 H ( n ) = n H(n)=n H(n)=n 这一估价函数。

代码

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#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 R[8][7]={	{ 0, 2, 6,11,15,20,22},
					{ 1, 3, 8,12,17,21,23},
					{10, 9, 8, 7, 6, 5, 4},
					{19,18,17,16,15,14,13},
					{23,21,17,12, 8, 3, 1},
					{22,20,15,11, 6, 2, 0},
					{13,14,15,16,17,18,19},
					{ 4, 5, 6, 7, 8, 9,10}};
const int M[8]={6,7,8,11,12,15,16,17};
string ans,tmp;
char cur[100003];
int A[24],S,Len;

int H(int k)
{
	int res=0;
	FOR(i,0,7)res+=((k>>M[i]&1)==0);
	return res;
}
void chk_ans(int s,int len)
{
	tmp="";
	FOR(i,0,len-1)tmp+=cur[i];
	if(len<Len||len==Len&&tmp<ans)ans=tmp,S=s,Len=len;
}

bool dfs(int k,int res,int s,int len)
{
	if(H(k)>res)return 0;
	if(res==0){chk_ans(s,len);return 1;};
	FOR(i,0,7)
	{
		int _k=k;bool c=k>>R[i][0]&1;
		FOR(j,0,5)
		{
			if(_k&(1<<R[i][j]))_k^=(1<<R[i][j]);
			if(_k&(1<<R[i][j+1]))_k|=(1<<R[i][j]);
		}
		if(_k&(1<<R[i][6]))_k^=(1<<R[i][6]);
		if(c)_k|=(1<<R[i][6]);
		cur[len]='A'+i;
		if(dfs(_k,res-1,s,len+1))return 1;
	}
	return 0;
}

int main()
{
	while(scanf("%d",&A[0])&&A[0])
	{
		FOR(i,1,23)scanf("%d",&A[i]);
		Len=2e9;
		for(int i=0;Len==2e9;i++)FOR(j,1,3)
		{
			int num=0;
			FOR(k,0,23)if(A[k]==j)num|=1<<k;
			dfs(num,i,j,0);
		}
		if(Len==0)puts("No moves needed");
		else cout<<ans<<endl;
		cout<<S<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值