思路:裸的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;
} 

如有不当之处欢迎指出!

HDU - 1584 IDA*的更多相关文章

  1. hdu 2234(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2234 思路:IDA*可以搞,借鉴的是大牛的启发式函数h(): 可以考虑把每一行上的数转化成相同的,或者 ...

  2. hdu 1667(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1667 思路:大牛说是IDA*的入门题=.=构造h()=8-max(1,2,3);  max(1,2,3 ...

  3. HDU 1584:蜘蛛牌(DFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1584 题意:要让小的牌放到大的牌上面最少移动的距离. 思路:看成让大的牌放在小的牌上面了...用一个标记数组vi ...

  4. HDU - 3567 IDA* + 曼哈顿距离 + 康托 [kuangbin带你飞]专题二

    这题难度颇大啊,TLE一天了,测试数据组数太多了.双向广度优先搜索不能得到字典序最小的,一直WA. 思路:利用IDA*算法,当前状态到达目标状态的可能最小步数就是曼哈顿距离,用于搜索中的剪枝.下次搜索 ...

  5. hdu 2918(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2918 思路:这道题与前面几道类似,可以说是被秒杀了!!!构造启发式函数h()=(cnt+3)/4(cn ...

  6. hdu 1813(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1813 思路:首先bfs预处理出‘0’到边界点最短距离,然后构造 h() 为所’0‘点逃离迷宫的最少步数 ...

  7. hdu 1560(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 思路:关键是启发式函数h()的构造,我们可以这样想:每次给主串增加一个字符和字符串的最后一位比较 ...

  8. 蜘蛛牌(hdu 1584 DFS)

    蜘蛛牌 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. HDU 1584 蜘蛛牌

    题解:纸牌只能移到比其大一的纸牌上,所以移动方向是定的,那么,就只有选择移动先后的问题了,对于决定要移的纸牌,比如1,如果2,3,4都是visited的状态,那么1一定是要移动到5的,因为2,3,4一 ...

随机推荐

  1. OpenCv函数学习(一)

    Intel Image Processing Library (IPL) typedef struct _IplImage { int nSize; /* IplImage大小 */ int ID; ...

  2. SSM与jsp传递实体类

    jsp传controller Controller: @RequestMapping({"/user"}) public void registerUser(User uu) th ...

  3. 转载-Linux Shell 数组建立及使用技巧

    转载自:http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html 如侵犯版权,请联系我删除 linux shell在编程方面比win ...

  4. 《剑指offer》数组中出现次数超过数组长度一半的数字

    题目: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2.如 ...

  5. IO (一)

    1 IO(Input Output)流概述 IO流用来处理设备之间的数据传输. java对数据的操作是通过流的方式. java用于操作流的对象都在IO包中. 流按操作数据分为两种:字节流和字符流. 流 ...

  6. awk 的 pattern(模式)

    我们知道, awk程序由一系列 pattern 以及与之对应的 action 组成的 rule 组成,rule之间用";"分号隔开, 一条输入记录与 pattern 匹配则执行与之 ...

  7. JAVA并发编程学习笔记------锁顺序死锁

    一.需求描述: 将资金从一个账户转移到另一个账户. 二.程序实现: (1)账户类: public class Account { private long account; public Accoun ...

  8. oracle用户、权限操作

    oracle用户操作,权限操作: 1.创建用户,并让用户默认表空间为tb1: create user 用户名 identified by 密码 default namespace tb1 2.授权: ...

  9. Hadoop学习笔记二

    一.设置无密码sudo权限,不用在普通用户和root用户间来回切换 chmod u+w /etc/sudoers vim /etc/sudoers #首行添加如下的内容: hadoop ALL=(ro ...

  10. BZOJ 1176: [Balkan2007]Mokia [CDQ分治]

    题意: 有一个n * n的棋盘,每个格子内有一个数,初始的时候全部为0.现在要求维护两种操作: 1)Add:将格子(x, y)内的数加上A. 2)Query:询问矩阵(x0, y0, x1, y1)内 ...