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

#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. day20-python之装饰器

    1.装饰器 #!/usr/bin/env python # -*- coding:utf-8 -*- import time def cal(l): start_time=time.time() re ...

  2. url编码&&PHP大法&&这个看起来有点简单&&HTML 中有用的字符实体

    URL编码 Url编码通常也被称为百分号编码(Url Encoding,also known as percent-encoding),是因为它的编码方式非常简单,使用%百分号加上两位的字符——012 ...

  3. 【Linux】网卡配置与绑定

    Redhat Linux的网络配置,基本上是通过修改几个配置文件来实现的. 虽然也可以用ifconfig来设置IP,用route来配置默认网关,用hostname来配置主机名,但是重启后会丢失. 相关 ...

  4. Python基础之yield,匿名函数,包与re模块

    一.表达式形式的yield 1.另外一种形式的yield def deco(func): def wrapper(*arges, **kwargs): res = func(*arges, **kwa ...

  5. 【13】javascript跨域通信

    javascript跨域通信 同源:两个文档同源需满足 协议相同 域名相同 端口相同 跨域通信方法: 01,通过设置img,script,link,iframe元素的src,href属性为目标url. ...

  6. Eclipse如何创建模拟器

    Eclipse如何创建模拟器下载地址:http://developer.android.com/sdk/index.html#downloadJDK安装包: 1, 打开安卓模拟器控制台(windows ...

  7. [adb 命令学习篇] adb 命令总结

    https://testerhome.com/topics/2565 Android 常用 adb 命令总结 针对移动端 Android 的测试, adb 命令是很重要的一个点,必须将常用的 adb ...

  8. Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring

    D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  9. SQL indexOf、lastIndexOf

    DECLARE @Name NVARCHAR (50)SET @Name = 'abcd.12345.efght' DECLARE @Position INT --sql first indexofS ...

  10. BZOJ2654 tree 【二分 + 最小生成树】

    题目 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. 输入格式 第一行V,E,need分别表示点数,边数和需要的白色边数. 接下来E行, ...