#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)的更多相关文章

  1. 紫书p199 八数码(BFS,hash)

    八数码问题  紫书上的简单搜索  渣渣好久才弄懂 #include<cstdio> #include<cstring> using namespace std; const i ...

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

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

  3. A*算法 -- 八数码问题和传教士过河问题的代码实现

    前段时间人工智能的课介绍到A*算法,于是便去了解了一下,然后试着用这个算法去解决经典的八数码问题,一开始写用了挺久时间的,后来试着把算法的框架抽离出来,编写成一个通用的算法模板,这样子如果以后需要用到 ...

  4. 八数码问题:C++广度搜索实现

    毕竟新手上路23333,有谬误还请指正. 课程设计遇到八数码问题(这也是一坨),也查过一些资料并不喜欢用类函数写感觉这样规模小些的问题没有必要,一开始用深度搜索却发现深搜会陷入无底洞,如果设定了深度限 ...

  5. ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)

    八数码问题也称为九宫问题.(本想查查历史,结果发现居然没有词条= =,所谓的历史也就不了了之了) 在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个 ...

  6. BFS(八数码) POJ 1077 || HDOJ 1043 Eight

    题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...

  7. 双向广搜+hash+康托展开 codevs 1225 八数码难题

    codevs 1225 八数码难题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description Yours和zero在研究A*启 ...

  8. UVALive 6665 Dragon’s Cruller --BFS,类八数码问题

    题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的). 分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可. 代码: #include <i ...

  9. P1379 八数码问题

    aoapc上的八数码问题,在luogu上也有类似的题,p1379,经典题目,lrj给出了一个算法,同时给出了三种判重的方法.本来想用std::queue改写一下,但是出了各种问题,只好抄代码ac掉这道 ...

随机推荐

  1. HBase配置项详解

    hbase.tmp.dir:本地文件系统的临时目录,默认是java.io.tmpdir/hbase−java.io.tmpdir/hbase−{user.name}: hbase.rootdir:hb ...

  2. js前端的各种面试题

    转载:http://bbs.blueidea.com/thread-3107428-1-1.html 1.截取字符串abcdefg的efg //alert('abcdefg'.substring(4) ...

  3. STM32 C语言,端口映射

    static XX 有记忆的定义 typedef XX 可以多次定义一个 #ifedf XXX XXX(程序段1) #else XXX(程序段2)

  4. poj-1410 Intersection

    计算几何的题目, 学cv的要做一下.poj 地址: http://poj.org/problem?id=1410 题意:判断一个直线段,是否与一个矩形有相交点. 解决方案: 判断矩形的每一条边是否与直 ...

  5. hihocoder1241 Best Route in a Grid

    题目链接:hihocoder 1241 题意: n*n的格阵,每个方格内有一个数字.蚂蚁从左上角走到右下角,数字是零的方格不能走,只能向右向下走.蚂蚁走的路径上全部方格的的乘积为s,要使s低位0的个数 ...

  6. 东大OJ 2SAT 异或

    看了十年才懂懂了十年才会会了十年才会写写了十年才写完写完了十年才能改对 #include<stdio.h> #include<string.h> struct res{ int ...

  7. ASP.NET配置Ueditor编辑器上传图片路径

    1.配置ueditor/editor_config.js文件,将 //图片上传配置区 ,imageUrl:URL+"net/imageUp.ashx" //图片上传提交地址 ,im ...

  8. 链栈的C语言实现

    /* 功能:栈的链表实现 Author:lxm Date: 20160511 */ #include <stdio.h> #include <stdlib.h> #define ...

  9. JVM垃圾收集器介绍

    垃圾回收算法是GC的方法论,垃圾收集器就是内存回收的具体实现. 一.Serial 收集器 单线程收集器,在进行GC时,必须暂停所有的工作线程(Stop The World),直到GC收集结束. 缺点: ...

  10. nginx安装(1) – ttlsa教程系列之nginx

    1.必要软件准备 安装pcre 为了支持rewrite功能,我们需要安装pcre   1 # yum install pcre* //如过你已经装了,请跳过这一步 安装openssl 需要ssl的支持 ...