八数码问题(紫薯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掉这道 ...
随机推荐
- React Native 在现有项目中的探路
移动开发中,native开发性能和效果上无疑是最好的. 但是在众多的情况下,native开发并不是最优的选择.当需求经常改动的时候,当预算有限的时候,当deadline很近的时候,native开发的成 ...
- 一些实用的sublime快捷键以及初始设置
一些常用快捷键 Ctrl + N-------------------新建 Ctrl + F-------------------查找 Ctrl+Shift +k -----------删除一行 Ct ...
- Android开发环境部署
引言 在windows系统中安装Android的开发环境,将分为五个步骤来完成: 第一步:安装JDK 第二步:配置Windows上JDK的变量环境 第三步: 下载安装Eclipse 第四步:下载安 ...
- 关于Hellas和Greece
一直以来我就好奇,为什么希腊的中文名字“希腊”和英文名字”Greece”听起来都不像(就像“德国”不像“Germany”一样),而且,为什么在很多体育比赛中看到希腊运动员的衣服上都是“Hellas”, ...
- mac 设置阿里企业邮箱
接收邮件服务器:pop3.mxhichina.com或pop3.您的域名,端口:110 发送邮件服务器:smtp.mxhichina.com或smtp.您的域名,端口:25 IMAP协议设置 接收邮件 ...
- STL数组处理常用函数
reverse(a,a+n)反转 sort(a,a+n,cmp)排序 unique(a,a+n,cmp)对于有序集合进行去重,返回新数组最后一个元素的指针 next_permutatoin(a,a+n ...
- linux基础-第十二单元 硬盘分区、格式化及文件系统的管理一
第十二单元 硬盘分区.格式化及文件系统的管理一 硬件设备与文件名的对应关系 硬盘的结构及硬盘分区 为什么进行硬盘分区 硬盘的逻辑结构 Linux系统中硬盘的分区 硬盘分区的分类 分区数量的约束 使用f ...
- 0921MySQL 报错 ERROR 1290 (HY000): running with the --secure-file-priv
http://blog.itpub.net/26506993/viewspace-2121850/ mysql> show variables like '%secure%';+-------- ...
- [Mindjet MindManager]思维导图的快捷键操作
来源于:http://www.cnblogs.com/whylaughing/p/5530935.html Mindjet MindManager(思维导图) 快捷键如下: Insert or CTR ...
- Ubuntu14.04下jdk的安装
1.下载JDK目前最新的JDK版本是:Java SE Development Kit 8u52.解压安装我们把JDK安装到这个路径:/usr/lib/jvm如果没有这个目录(第一次当然没有),我们就新 ...