http://acm.split.hdu.edu.cn/showproblem.php?pid=1043

刚开始一脸蒙逼,看了题解之后,参考了A*算法。

参考:http://www.cnblogs.com/technology/archive/2011/05/26/2058842.html

还用到了变进制Hash全排列来判断结果是否满足条件。

参考:http://www.chinaunix.net/old_jh/23/1283459.html

第一次接触A*,感觉类似BFS,不同的是它可以更快地找到最优解。

最后要注意路径的保存,最终用dfs的printff输出。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdio>
#define MAX 322560
using namespace std; struct Node
{
int a[],g,h,num,Hash;
friend bool operator<(const Node x,const Node y)
{
return x.h+x.g>y.h+y.g;
}
}start; int HASH[]={,,,,,,,,};
int vis[],pre[],dis[] = {-,-,,};
int a[][]; int get_Hash(Node x)
{
int ans = ;
for(int i = ;i < ;i++)
{
int k = ;
for(int j =;j <i;j++)
{
if(x.a[j]>x.a[i]) k++;
}
ans += HASH[i]*k;
}
return ans;
} int get_h(Node x)
{ //»ñµÃ¹À¼Ûº¯ÊýH
int ans=;
for(int i=;i<;i++)
{
if(x.a[i] == ) continue;
ans += abs(i/-(x.a[i]-)/)+abs(i%-(x.a[i]-)%);
}
return ans;
} void printff(int h)
{
if(pre[h] == -) return;
printff(pre[h]);
switch (vis[h])
{
case : printf("u");
break;
case : printf("l");
break;
case : printf("d");
break;
case : printf("r");
break;
}
}
int main()
{
char str[];
while(gets(str))
{
memset(vis,-,sizeof(vis));
memset(pre,-,sizeof(pre));
int now = ,num = ;
for(int i = ;i < ;i++)
{
for(int j = ;j < ;j++,now++)
{
if(str[now] >= '' && str[now] <= '')
{
start.a[num++] = str[now]-'';
}
else if(str[now] == 'x')
{
start.num = num;
start.a[num++] = ;
}
else j--;
}
} num = ;
for(int i = ;i < ;i++)
{
for(int j = i+;j <;j++)
{
if(start.a[i] && start.a[j] && start.a[i]>start.a[j]) num++;
}
}
if(num%)
{
printf("unsolvable\n");
continue;
}
start.Hash = get_Hash(start);
if(start.Hash == MAX)
{
printf("\n");
continue;
}
start.g = ;
start.h = get_h(start);
vis[start.Hash] = -;
priority_queue<Node> q;
q.push(start);
while(!q.empty())
{
Node temp = q.top();
int xx = temp.num/,yy = temp.num%;
q.pop();
for(int i = ;i < ;i++)
{
Node x = temp;
x.num += dis[i];
if(yy == && i == ) continue;
if(yy == && i == ) continue;
if(x.num < || x.num > ) continue;
swap(x.a[x.num],x.a[temp.num]);
x.Hash = get_Hash(x);
if(vis[x.Hash] != -) continue;
x.g++;
x.h = get_h(x);
q.push(x);
pre[x.Hash] = temp.Hash;
vis[x.Hash] = i;
if(x.Hash == MAX) goto there;
}
}
there:
printff(MAX);
printf("\n");
}
}

HDU_1175_A*的更多相关文章

随机推荐

  1. shell脚本一键配置本地yum源

    效果如图: #!/bin/bash #Author:GaoHongYu #QQ: #Time:-- :: #Name:yumgz.sh #Version:V1. CKGZ=$( df -Th | gr ...

  2. linux防火墙之iptables

    linux防火墙之iptables 1.1.1 关于iptables简介 IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统.如果 Linux 系统连接到因特网或 ...

  3. 一起来立Flag吧!超炫的数据图表分析 2020 年 Java 技术趋势

    引言 2020 来了,第一批 00 后已经 20 岁了,95 后也到了晚婚的年龄,员外的头发也越来越少了,新的一年大家有立下了哪些 Flag ?小伙伴们别急着立 Flag,让员外帮你分析一下哪些技术正 ...

  4. 【一起学源码-微服务】Hystrix 源码三:Hystrix核心流程:Hystix降级、熔断等原理剖析

    说明 原创不易,如若转载 请标明来源! 欢迎关注本人微信公众号:壹枝花算不算浪漫 更多内容也可查看本人博客:一枝花算不算浪漫 前言 前情回顾 上一讲我们讲解了Hystrix在配合feign的过程中,一 ...

  5. 树 dfs暴力判环 题意转化

    以后还是要多做题啊 这一道题我把题目想的太简单了 用并查集做了一波 但是忘了一种情况 就是同一个树上可能会有环 这就不太对了 而且还不要忘了 一棵树的根节点是一个自环 也就是说这一题的答案就是    ...

  6. MySQL UTF-8 常用字符排序规则

    排序规则 解释 utf8_general_ci 不区分大小写 utf8_general_cs 区分大小写 utf8_bin 区分大小写,字符串每个字符串用二进制数据编译存储,且支持存储二进制数据

  7. 【UEFI】---BIOS中对Guid的使用以及Lib函数的使用总结

    ---恢复内容开始--- BIOS发展至今传统的汇编实现早已被抛弃,UEFI作为目前一套主流的标准定义接口,被广泛使用.之前被一些有关GUID和一些Lib函数的使用以及跨Pkg调用给折腾的不行,每次改 ...

  8. python3迭代器

    一.前提 1.dir()函数 dir()函数带参数时,返回参数的属性和方法列表:不带参数时,返回当前范围内变量.方法和定义的类型列表 # dir(参数):带参数,返回参数的属性和方法 s = '' p ...

  9. 异数OS-织梦师-PBFT(六) 走出区块链,加速破解PBFT

    . 异数OS-织梦师-PBFT(六) 走出区块链,加速破解PBFT 拜占庭 本文来自异数OS社区 github: https://github.com/yds086/HereticOS 异数OS社区Q ...

  10. 嵩天老师python网课爬虫实例1的问题和解决方法

    一,AttributeError: 'NoneType' object has no attribute 'children', 网页'tbody'没有子类 很明显,报错的意思是说tbody下面没有c ...