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;
}
如有不当之处欢迎指出!
HDU - 1584 IDA*的更多相关文章
- hdu 2234(IDA*)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2234 思路:IDA*可以搞,借鉴的是大牛的启发式函数h(): 可以考虑把每一行上的数转化成相同的,或者 ...
- hdu 1667(IDA*)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1667 思路:大牛说是IDA*的入门题=.=构造h()=8-max(1,2,3); max(1,2,3 ...
- HDU 1584:蜘蛛牌(DFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1584 题意:要让小的牌放到大的牌上面最少移动的距离. 思路:看成让大的牌放在小的牌上面了...用一个标记数组vi ...
- HDU - 3567 IDA* + 曼哈顿距离 + 康托 [kuangbin带你飞]专题二
这题难度颇大啊,TLE一天了,测试数据组数太多了.双向广度优先搜索不能得到字典序最小的,一直WA. 思路:利用IDA*算法,当前状态到达目标状态的可能最小步数就是曼哈顿距离,用于搜索中的剪枝.下次搜索 ...
- hdu 2918(IDA*)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2918 思路:这道题与前面几道类似,可以说是被秒杀了!!!构造启发式函数h()=(cnt+3)/4(cn ...
- hdu 1813(IDA*)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1813 思路:首先bfs预处理出‘0’到边界点最短距离,然后构造 h() 为所’0‘点逃离迷宫的最少步数 ...
- hdu 1560(IDA*)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 思路:关键是启发式函数h()的构造,我们可以这样想:每次给主串增加一个字符和字符串的最后一位比较 ...
- 蜘蛛牌(hdu 1584 DFS)
蜘蛛牌 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 1584 蜘蛛牌
题解:纸牌只能移到比其大一的纸牌上,所以移动方向是定的,那么,就只有选择移动先后的问题了,对于决定要移的纸牌,比如1,如果2,3,4都是visited的状态,那么1一定是要移动到5的,因为2,3,4一 ...
随机推荐
- Servlet--HttpServletRequest一些不常用的方法
我们在使用Servlet和表单进行交互的时候,不管是传参和接参经常要写一些路径.关于具体的Servlet的传参和接参我后面会有详细的整理,这里先整理一下不怎么常用的到一些HttpServletRequ ...
- Arrays.sort解析
Arrays.sort()解读 在学习了排序算法之后, 再来看看 Java 源码中的, Arrays.sort() 方法对于排序的实现. 都是对基本数据类型的排序实现, 下面来看看这段代码: Arra ...
- Tomcat xxx unbound
从别的地方import的代码 .出现apache unbound 第一步:选中项目右键Build Path->Configure Build Path-->选中Tomcat 7.0 unb ...
- 【转】<string> <string.h> <cstring>的区别
#include < string.h > void main() { string aaa = " abcsd d " ; printf( " lookin ...
- 【Thinkphp 5】 如何引入extend拓展文件
extend/maile/cc.php 文件目录 cc文件 必须要加上命名空间,如下 cc.php文件内容如下: namespace maile; //命名空间 maile是文件夹名称 class C ...
- 开发JQuery插件(转)
教你开发jQuery插件(转) 阅读目录 基本方法 支持链式调用 让插件接收参数 面向对象的插件开发 关于命名空间 关于变量定义及命名 压缩的好处 工具 GitHub Service Hook 原 ...
- Codeforce A. Quasi-palindrome
A. Quasi-palindrome time limit per test 1 second memory limit per test 256 megabytes input standard ...
- java文件监控[转]
原文链接:http://blog.csdn.net/dancen/article/details/7786987#comments 问题:存在两个文件目录,且称之为源目录和目标目录,需要不定期将源目录 ...
- BZOJ 3786: 星系探索 [伪ETT]
传送门 数据,标程 题意: 一颗有根树,支持询问点到根路径权值和,子树加,换父亲 欧拉序列怎么求路径权值和? 一个点的权值只会给自己的子树中的点贡献,入栈权值正出栈权值负,求前缀和就行了! 和上题一样 ...
- BZOJ 2419: 电阻 [高斯消元 物理]
http://www.lydsy.com/JudgeOnline/problem.php?id=2419 题意: n个点m个电阻构成一张图,求1到n的等效电阻 第一节课看一道题弃疗,于是来做这道物理题 ...