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

#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. Django ORM字段和字段参数

    Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...

  2. nw335 debian sid x86-64 -- 2 驱动的方式

    1 linux内核自带 2 realtek 提供的官方驱动 3 使用xp的驱动 4 第三方驱动(现在成功的,最好的方式)

  3. Java-从一个字符串获取子字符串

    substring函数 package com.tj; public class MyClass implements Cloneable { public static void main(Stri ...

  4. Java-获取Class对象的名称

    package com.tj; public class MyClass2 { public static void main(String[] args) { Class cls = java.la ...

  5. STM32F407 DAC 个人笔记

    DAC框图 VDDA:模拟电源输入 VSSAL:模拟电源接地输入 Vref+:正模拟参考电压输入 -------------- DORx:输入数字寄存器 DAC_OUT:模拟输出通道 DAC_OUT1 ...

  6. Appium解锁九宫格(TouchAction)

    TouchAction 1.源码可以在这个路径找到:Lib\site-packages\appium\webdriver\common\touch_action.py class TouchActio ...

  7. Axure:从单一评价方式到用户自由选择

    导读: 亲,还记得淘宝对货物的评价方式吗?还记得对快递哥的评价方式吗? 1,经典五星评:                                                         ...

  8. 九度oj 题目1114:神奇的口袋

    题目描述: 有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40.John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an.John可以从这些物品中 ...

  9. 如何部署 sources and javadoc jars

    mvn org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy-file -Durl=file:///home/me/m2-repo \ - ...

  10. Codeforces633G - Yash And Trees

    Portal Description 给出一个\(n(n\leq10^5)\)个点的带点权树,以\(1\)为根:以及正整数\(m(m\leq10^3)\).进行\(q(q\leq10^5)\)次操作: ...