八数码问题(紫薯P199)
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;
typedef int State[];
const int maxstate = ;
State st[maxstate], goal;
int dist[maxstate];
const int dx[] = {-, , , };
const int dy[] = {, , -, };
set<int> vis;
/*
void init_lookup_table()
{
vis.clear();
}
int try_to_insert(int s)
{
int v = 0;
for(int i = 0; i < 9; i++)
v = v * 10 + st[s][i];
if(vis.count(v))
return 0;
vis.insert(v);
return 1;
}
*/
const int hashsize = ;
int head[hashsize], Next[maxstate];
void init_lookup_table()
{
memset(head, , sizeof(head));
}
int Hash(State &s)
{
int v = ;
for(int i = ; i < ; i++)
v = v * + s[i];
return v % hashsize;
}
int try_to_insert(int s)
{
int h = Hash(st[s]);
int u = head[h];
while(u)
{
if(memcmp(st[u], st[s], sizeof(st[s])) == )
return ;
u = Next[u];
}
Next[s] = head[h];
head[h] = s;
return ;
}
int bfs()
{
init_lookup_table();
int first = , last = ;
dist[] = ;
while(first < last)
{
State &s = st[first];
if(memcmp(goal, s, sizeof(s)) == )
return first;
int z;
for(z = ; z < ; z++)
if(s[z] == )
break;
int x = z / ;
int y = z % ;
for(int i = ; i < ; i++)
{
int fx = x + dx[i];
int fy = y + dy[i];
int fz = fx * + fy;
if(fx >= && fy >= && fx < && fy < )
{
State &t = st[last];
memcpy(&t, &s, sizeof(s));
t[fz] = s[z];
t[z] = s[fz];
dist[last] = dist[first] + ;
if(try_to_insert(last))
last++;
}
}
first++;
}
return ;
}
int main()
{
for(int i = ; i < ; i++)
scanf("%d", &st[][i]);
for(int i = ; i < ; i++)
scanf("%d", &goal[i]);
int ans = bfs();
if(ans > )
printf("%d\n", dist[ans]);
else
printf("-1\n");
return ;
}
/*
2 6 4 1 3 7 0 5 8
8 1 5 7 3 6 4 0 2
*/
八数码问题(紫薯P199)的更多相关文章
- 紫书p199 八数码(BFS,hash)
八数码问题 紫书上的简单搜索 渣渣好久才弄懂 #include<cstdio> #include<cstring> using namespace std; const i ...
- HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)
题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...
- A*算法 -- 八数码问题和传教士过河问题的代码实现
前段时间人工智能的课介绍到A*算法,于是便去了解了一下,然后试着用这个算法去解决经典的八数码问题,一开始写用了挺久时间的,后来试着把算法的框架抽离出来,编写成一个通用的算法模板,这样子如果以后需要用到 ...
- 八数码问题:C++广度搜索实现
毕竟新手上路23333,有谬误还请指正. 课程设计遇到八数码问题(这也是一坨),也查过一些资料并不喜欢用类函数写感觉这样规模小些的问题没有必要,一开始用深度搜索却发现深搜会陷入无底洞,如果设定了深度限 ...
- ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)
八数码问题也称为九宫问题.(本想查查历史,结果发现居然没有词条= =,所谓的历史也就不了了之了) 在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个 ...
- BFS(八数码) POJ 1077 || HDOJ 1043 Eight
题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...
- 双向广搜+hash+康托展开 codevs 1225 八数码难题
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
- UVALive 6665 Dragonâs Cruller --BFS,类八数码问题
题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的). 分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可. 代码: #include <i ...
- P1379 八数码问题
aoapc上的八数码问题,在luogu上也有类似的题,p1379,经典题目,lrj给出了一个算法,同时给出了三种判重的方法.本来想用std::queue改写一下,但是出了各种问题,只好抄代码ac掉这道 ...
随机推荐
- 从setTimeout谈JavaScript运行机制
从setTimeout说起 众所周知,JavaScript是单线程的编程,什么是单线程,就是说同一时间JavaScript只能执行一段代码,如果这段代码要执行很长时间,那么之后的代码只能尽情地等待它执 ...
- 流量工程 traffic engineering (TE)
什么是流量工程 流量工程是指根据各种数据业务流量的特性选取传输路径的处理过程.流量工程用于平衡网络中的不同交换机.路由器以及链路之间的负载. [编辑] 流量工程的内容 流量工程在复杂的网络环境中,控制 ...
- 服务链(Service Chaining,or Service Function Chaining,SFC,功能服务链)
Software-configured service chaining provides the capability to dynamically include best-of-b ...
- Adaboost 算法的原理与推导
0 引言 一直想写Adaboost来着,但迟迟未能动笔.其算法思想虽然简单“听取多人意见,最后综合决策”,但一般书上对其算法的流程描述实在是过于晦涩.昨日11月1日下午,邹博在我组织的机器学习班第8次 ...
- 自定义getElementByClass
DOM已经实现了getElementByClass,这个功能内部是怎么实现的呢 js代码及如何使用: function getElementByClass(className,parentNode){ ...
- go linux 学习记录
1 yum install mercurial 安装mercurial包 2 yum install git 安装git包 3 yum install gcc 安装gcc 4 然后就可以下载gola ...
- html 文本超过显示省略号
display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2;//显示行数 word-break: break-a ...
- android 资讯阅读器
最近找申请到了一个不错的接口 , 非常适合拿来写一个资讯类的app. 现在着手写,随写随更.也算是抛砖引玉.烂尾请勿喷.╭(╯^╰)╮ android 资讯阅读器 第一阶段目标样式(滑动切换标签 , ...
- ART、JIT、AOT、Dalvik之间的关系
原文地址: https://github.com/ZhaoKaiQiang/AndroidDifficultAnalysis/blob/master/10.ART%E3%80%81JIT%E3%80% ...
- svn 的备份还原
备份脚本如下: #!/bin/bash ############################################################################### ...