#include<iostream>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#define INF 0x3f3f3f3f
//const int maxn = 1e6 + 5;
const double PI = acos(-1.0);
typedef long long ll;
using namespace std; char ss[];
int ans[][] = {
{,,,},
{,,,},
{,,,},
{,,,}
};
int a[][];
int k, judge;
int dx[] = { ,,-, };
int dy[] = { ,,,- }; int check() {
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if (ans[i][j] != a[i][j]) return ;
}
}
return ;
} int test(int step) {
int cnt = ;
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if (ans[i][j] != a[i][j]) {
if (++cnt + step > k) return ;
}
}
}
return ;
} void A_star(int step, int x, int y, int pre) {
if (step == k) {
if (check()) judge = ; return;
}
if (judge) return;
for (int i = ; i < ; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (xx < || xx> || yy < || yy> || pre + i == ) continue;
swap(a[x][y], a[xx][yy]);
if (test(step) && !judge) A_star(step + , xx, yy, i);
swap(a[x][y], a[xx][yy]);
}
} int main() {
int x, y;
scanf("%s",ss);
for (int i = ; i < ; i++) {
a[i / + ][i % + ] = ss[i] - '';
if (ss[i] - '' == ) x = i / + , y = i % + ;
}
if (check()) {
printf("0\n");
}
else {
while (++k) {
A_star(, x, y, -);
if (judge) {
printf("%d", k);
break;
}
}
}
return ;
}

八数码问题 IDA*搜索的更多相关文章

  1. [luogu]P1379 八数码难题[广度优先搜索]

    八数码难题 ——!x^n+y^n=z^n 我在此只说明此题的一种用BFS的方法,因为本人也是初学,勉勉强强写了一个单向的BFS,据说最快的是IDA*(然而蒟蒻我不会…) 各位如果想用IDA*的可以看看 ...

  2. 八数码(IDA*算法)

    八数码 IDA*就是迭代加深和A*估价的结合 在迭代加深的过程中,用估计函数剪枝优化 并以比较优秀的顺序进行扩展,保证最早搜到最优解 需要空间比较小,有时跑得比A*还要快 #include<io ...

  3. codevs1225八数码难题(搜索·)

    1225 八数码难题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description Yours和zero在研究A*启 ...

  4. POJ1077&&HDU1043(八数码,IDA*+曼哈顿距离)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30127   Accepted: 13108   Special ...

  5. HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)

    题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...

  6. HUD 1043 Eight 八数码问题 A*算法 1667 The Rotation Game IDA*算法

    先是这周是搜索的题,网站:http://acm.hdu.edu.cn/webcontest/contest_show.php?cid=6041 主要内容是BFS,A*,IDA*,还有一道K短路的,.. ...

  7. 八数码问题:C++广度搜索实现

    毕竟新手上路23333,有谬误还请指正. 课程设计遇到八数码问题(这也是一坨),也查过一些资料并不喜欢用类函数写感觉这样规模小些的问题没有必要,一开始用深度搜索却发现深搜会陷入无底洞,如果设定了深度限 ...

  8. HDU 1043 八数码(A*搜索)

    在学习八数码A*搜索问题的时候须要知道下面几个点: Hash:利用康托展开进行hash 康托展开主要就是依据一个序列求这个序列是第几大的序列. A*搜索:这里的启示函数就用两点之间的曼哈顿距离进行计算 ...

  9. hdu 1043 Eight 经典八数码问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...

随机推荐

  1. 两台W7系统的电脑,A电脑可以ping通B电脑,B电脑ping不通A电脑。

    https://zhidao.baidu.com/question/1946500242424659908.html 打开控制面板-系统和安全-防火墙-允许程序-文件和打印机共享(勾选) 局域网共享是 ...

  2. c++ 读取、保存单张图片

    转载:https://www.jb51.net/article/147896.htm 实际上就是以二进制形式打开文件,将数据保存到内存,在以二进制形式输出到指定文件.因此对于有图片的文件,也可以用这种 ...

  3. redis 高级学习和应用场景

    redis 高级学习 1.redis 复制 2.redis 集群 3.哨兵机制 4.spring 与哨兵结合 5.数据恢复与转移 6.redis 的阻塞分析 redis 实战 1. 数据缓存(热点数据 ...

  4. Python开发之Anconda环境搭建

    Python的强大之处在于它的应用范围广泛,遍及人工智能.科学计算.web开发.系统运维.大数据及云计算等,实现其强大功能的前提,就是Python具有数量庞大且功能相对完善的标准库和第三方库.通过对库 ...

  5. ubutun18 install ibus-pinyin

    ref: https://www.cnblogs.com/asmer-stone/p/9069866.html Step1 $ sudo apt-get install ibus-pinyin

  6. requests---requests封装请求类型

    我们在做自动化的时候,肯定是代码越简洁越好,代码重复量越少越好,这里呢,我们可以通过把requests的请求类型都封装起来,这样编写用例的时候可以直接进行请求 requests方法封装 我们通常用的最 ...

  7. mysql将一个表拆分成多个表(一)(转载)

    转载 直接根据数据量进行拆分 有一个5000条数据的表,要把它变成没1000条数据一个表的5等份. 假设:表名:xuesi 主键:kidxuesi共有5000条数据,kid从1到5000自动增长题目: ...

  8. 关于length、length()、size()

    length:属性,数组的属性. length(): String的方法,方法体里面是  return value.length; size():集合如list.set.map的方法,返回元素个数.

  9. 吴裕雄--天生自然python爬虫:使用requests模块的get和post方式抓取中国旅游网站和有道翻译网站翻译内容数据

    import requests url = 'http://www.cntour.cn/' strhtml = requests.get(url) print(strhtml.text) URL='h ...

  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:用于要弹出信息的按钮

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...