八数码问题  紫书上的简单搜索  渣渣好久才弄懂

#include<cstdio>
#include<cstring>
using namespace std;
const int M = 1000003;
int x[4] = { -1, 1, 0, 0}, y[4] = {0, 0, -1, 1};
int dis[M], h[M], s[M][9], e[9]; int aton(int a[])
{
int t = 0;
for(int i = 0; i < 9; ++i)
t = t * 10 + a[i];
return t;
} int search_hash(int a[])
{
int t = aton(a), p = t % M;
while(h[p])
{
if(h[p] == t) return p;
++p;
if(p >= M) p = 0;
}
return p;
} int bfs()
{
memset(h, 0, sizeof(h));
int fro = 1, rear = 2, r, c, k = 0, p;
int t[9], tmp, cur, nr, nc, nk; while(fro < rear)
{
memcpy(t, s[fro], sizeof(t));
cur = search_hash(t);
if(memcmp(t, e, sizeof(t)) == 0) return dis[cur];
for(k = 0; t[k];) ++k;
r = k / 3, c = k % 3;
for(int i = 0; i < 4; ++i)
{
memcpy(t, s[fro], sizeof(t));
nr = r + x[i], nc = c + y[i], nk = nr * 3 + nc;
if(nr < 0 || nr > 2 || nc < 0 || nc > 2) continue;
tmp = t[nk];
t[nk] = 0;
t[k] = tmp;
p = search_hash(t);
if(h[p] == 0)
{
h[p] = aton(t);
dis[p] = dis[cur] + 1;
memcpy(s[rear], t, sizeof(t));
++rear;
}
}
++fro;
}
return -1;
} int main()
{
while(~scanf("%d", &s[1][0]))
{
memset(dis, 0, sizeof(dis));
for(int i = 1; i < 9; ++i)
scanf("%d", &s[1][i]);
for(int i = 0; i < 9; ++i)
scanf("%d", &e[i]);
h[aton(s[1]) % M] = aton(s[1]);
int ans = bfs();
if(ans != -1)
printf("%d\n", ans);
else printf("No solution\n");
}
return 0;
}
/*
2 6 4 1 3 7 0 5 8
8 1 5 7 3 6 4 0 2
2 3 4 1 5 0 7 6 8
1 2 3 4 5 6 7 8 0
*/

紫书p199 八数码(BFS,hash)的更多相关文章

  1. hdu-1043(八数码+bfs打表+康托展开)

    参考文章:https://www.cnblogs.com/Inkblots/p/4846948.html 康托展开:https://blog.csdn.net/wbin233/article/deta ...

  2. HDU1043 八数码(BFS + 打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 , 康托展开 + BFS + 打表. 经典八数码问题,传说此题不做人生不完整,关于八数码的八境界 ...

  3. code1225 八数码Bfs

    Bfs搜索 1.把棋盘直接作为状态: #include<iostream> #include<cstring> #include<queue> #include&l ...

  4. POJ1077 八数码 BFS

    BFS 几天的超时... A*算法不会,哪天再看去了. /* 倒搜超时, 改成顺序搜超时 然后把记录路径改成只记录当前点的操作,把上次的位置记录下AC..不完整的人生啊 */ #include < ...

  5. luogu_1379 八数码难题

    八数码-->BFS+set #include<iostream> #include<cstdlib> #include<cstdio> #include< ...

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

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

  7. Poj 1077 eight(BFS+全序列Hash解八数码问题)

    一.题意 经典的八数码问题,有人说不做此题人生不完整,哈哈.给出一个含数字1~8和字母x的3 * 3矩阵,如: 1  2  X            3 4  6            7  5  8 ...

  8. 洛谷 P1379 八数码难题 Label:判重&&bfs

    特别声明:紫书上抄来的代码,详见P198 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给 ...

  9. 【算法】BFS+哈希解决八数码问题

    15拼图已经有超过100年; 即使你不叫这个名字知道的话,你已经看到了.它被构造成具有15滑动砖,每一个从1到15上,并且所有包装成4乘4帧与一个瓦块丢失.让我们把丢失的瓷砖“X”; 拼图的目的是安排 ...

随机推荐

  1. 《零基础入门学习Python》【第一版】视频课后答案第006讲

    python中被看作假:FALSE  none 0  ‘ ’  " "  ( ) [ ] { },其他一切都被解释为真 测试题答案: 0.Python 的 floor 除法现在使用 ...

  2. (原)剑指offer跳台阶和矩形覆盖

    跳台阶 时间限制:1秒空间限制:32768K 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法.   分析同样为斐波那契数列边形这样的题肯定有公式 设 ...

  3. Linux等待队列与唤醒

    1.数据结构 1.1等待队列头 struct __wait_queue_head { spinlock_t lock; struct list_head task_list; }; typedef s ...

  4. failed to execute goal org.apache.maven.plugins:maven-archetype-plugin错误解决方法

    使用maven创建project时碰到如下错误: D:\codes\JSF>mvn archetype:create -DgroupId=com.tutorialspoint.test -Dar ...

  5. python基础学习笔记——collections模块

    在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict.namedtuple和Ord ...

  6. lua-helloworld

    write script file, a.lua: #!/usr/bin/lua print("hello world!") add excutable prperty to th ...

  7. Thread.getContextClassLoader() is null

    Java threads created from JNI code in a non-java thread have null ContextClassloader unless the crea ...

  8. shell的echo命令

    echo是Shell的一个内部指令,用于在屏幕上打印出指定的字符串.命令格式: echo arg 您可以使用echo实现更复杂的输出格式控制. 显示转义字符 echo "\"It ...

  9. POJ 2699 The Maximum Number of Strong Kings ——网络流

    一定存在一种最优方案,使得分数前几个人是SK 所以我们可以二分答案或者枚举,然后就是经典的网络流建模. 另:输入很Excited #include <cstdio> #include &l ...

  10. 线程池的使用。好文。mark【http://blog.csdn.net/rwecho/article/details/21157289】

    介绍new Thread的弊端及Java四种线程池的使用,对Android同样适用.本文是基础篇,后面会分享下线程池一些高级功能. 1.new Thread的弊端执行一个异步任务你还只是如下new T ...