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. ThreadLocal解析:父线程的本地变量不能传递到子线程详解

    众所周知,ThreadLocal类是java提供线程本地变量的工具类.但父线程的本地变量却不能被子线程使用,代码如下: public static void main(String[] args) { ...

  2. 天梯 L2 树的遍历(已知后序中序求层序)

    树的遍历 (25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数.第二行 ...

  3. Could not find a version that satisfies the requirement numpy>=1.7.0 (from pan das==0.17.0) (from versions: ) No matching distribution found for numpy>=1.7.0 (from pandas==0.17.0)

    今天晚上一直在安装pandas,天杀的,真的是太难了.后来发现提示: Could not find a version that satisfies the requirement numpy> ...

  4. java数据库学习路线和必学知识点!

    java数据库必学知识点! 分享一下数据库的学习路线和必学的知识点! 掌握mysql,Oracle在各个平台上的安装及使用 Mysql数据库基础 mysql概述.优点.运行原理及内存结构 mysql数 ...

  5. Redis高可用方案-哨兵与集群

    Redis高可用方案 一.名词解释   二.主从复制 Redis主从复制模式可以将主节点的数据同步给从节点,从而保障当主节点不可达的情况下,从节点可以作为 后备顶上来,并且可以保障数据尽量不丢失(主从 ...

  6. Vue 编程式的导航

    1.应用场景 在同一路由的情况下,不同的参数之间进行切换 注意:别忘记初始化路由页面 2.用法 a.定义方法 b.实现方法 c.初始化路由页面 3.案例 <template> <di ...

  7. Django2.0 配置 media

    1.setting.py文件 MEDIA_URL='/media/' MEDIA_ROOT=os.path.join(BASE_DIR,"media") 注意:MEDIA_ROOT ...

  8. Tomcat9乱码解决

    在tomcat的解压目录下找到conf,打开进入,logging.properties文件,在该文件中,修改 java.util.logging.ConsoleHandler.encoding = U ...

  9. 【转载】Notepad++源码分析

    在网上发现了一个哥们写了关于Notepad++源码的文章,不过就写了一就没有了,我就接着他的工作再说说吧! 大三了,也写了一点儿程序了,但是如果只是按照自己的思路写下去恐怕难以提高,于是准备开始阅读一 ...

  10. 用tensorflow的Eager执行模式

    一.即时执行模式 import tensorflow as tfimport tensorflow.contrib.eager as tfetfe.enable_eager_execution() a ...