思路:裸的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. Duilib学习(一)

    #pragma once #include <UIlib.h> using namespace DuiLib; #ifdef _DEBUG # ifdef _UNICODE # pragm ...

  2. c:if true、false都显示

    看了半天,最后发现jstl标签库没有引入! <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core ...

  3. alibaba架包FastJson使用例子

    alibaba的架包FastJson可以对json字符串进行快捷的类型转换.下面是一些各种类型转换的使用例子. 一.下载FastJson的架包,并导入项目中,如下: Maven项目pom.xml配置如 ...

  4. web.xml 中CharacterEncodingFilter类的学习

    过滤器配置 当前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码的问题 //编码方式配置 <filter> <fi ...

  5. Asp.net mvc 中View 的呈现(二)

    [toc] 上一节介绍了 Asp.net mvc 中除 ViewResult 外的所有的 ActionResult,这一节介绍 ViewResult. ViewResultBase ViewResul ...

  6. Codeforce A. Fair Game

    A. Fair Game time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  7. CSS初了解

    1.在网页中, html负责的是一个页面的结构 css(层叠式表)是网页中的数据样式 2.编写css代码方式: A: 在style标签中编写代码,只能用在本页面中,复用性不强. 格式:<styl ...

  8. mybatis No enum const class org.apache.ibatis.type.JdbcType.Integer

    mybatis报错:没有Integer这个类型的jdbcType值 原因:mybatis配置重的jdbaType类型要是大写的 如图所示:

  9. xBIM 基本的模型操作

    目录 xBIM 应用与学习 (一) xBIM 应用与学习 (二) xBIM 基本的模型操作 xBIM 日志操作 XBIM 3D 墙壁案例 xBIM 格式之间转换 xBIM 使用Linq 来优化查询 x ...

  10. Eclipse EE遇到问题记录

    Eclipse EE可以进行Java web的开发,下面记录了使用Eclipse EE调试时遇到的一些问题. 1.tomcat启动timeout的设置,双击server,主窗口就变为了server的配 ...