HDU - 1584 IDA*

          思路:裸的IDA*,估计当前状态至少需要多少距离才能达到目标状态,剪枝即可。每一墩牌只需记录其最上面和最下面的牌型即可完成移动。

AC代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int> 
typedef long long LL;
const int maxn = 10 + 2;
struct node{
	int top, bot;
}sta[maxn]; //只需记录其最下面和最下面的牌就行 

int get_h(node *a) {
	int cnt = 0, fir;
	for(int i = 0; i < 10; ++i) {
		if(a[i].bot) {
			fir = i;
			break;
		}
	}
	for(int i = fir+1; i < 10; ++i){
		if(a[i].bot) {
			cnt += i - fir;
			fir = i;
		}
	}
	return cnt;
}

int maxd, nextd; 
bool dfs(int dis) {
	int h = get_h(sta);
	if(h + dis > maxd) {
		nextd = min(nextd, h+dis);
		return false;
	}
	if(h == 0) return true;
	node old[maxn];
	memcpy(old, sta, sizeof(sta));
	for(int i = 0; i < 10; ++i) {
		if(sta[i].bot && sta[i].bot < 10) {
			int bot = sta[i].bot;
			for(int j = 0; j <= maxd-dis+i; ++j) {
				if(sta[j].top == bot + 1) { //将i移到j 
					sta[j].top = sta[i].top;
					sta[i].bot = sta[i].top = 0; //清空 
					if(dfs(dis+abs(i-j))) return true;
					break;
				}
			}
		} 
		memcpy(sta, old, sizeof(old));
	}
	return false;
}
int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		for(int i = 0; i < 10; ++i) {
			scanf("%d", &sta[i].top);
			sta[i].bot = sta[i].top;
		}
		for(maxd = 9; ;maxd = nextd) {
			nextd = maxd+1;
			if(dfs(0)) {
				printf("%d\n", maxd);
				break;
			}
		}
	}
	return 0;
} 

如有不当之处欢迎指出!

转载于:https://www.cnblogs.com/flyawayl/p/8305357.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值