#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. HTML5+JS 《五子飞》游戏实现(八)人机对战

    要想实现人机对战,就必须让电脑自动下棋,而且要知道自动去查找对方的棋子,看看有没有可以挑一对的,有没有可以夹一个的,这样下起来才有意思. 当电脑用户下完棋后,电脑应立即搜索用户的棋子,然后如果没有被吃 ...

  2. OpenStack 简介

    OpenStack是IaaS(基础设施即服务)组件,让任何人都可以自行建立和提供云端运算服务. 此外,OpenStack也用作建立防火墙内的"私有云"(Private Cloud) ...

  3. matlab中的xcorr 自相关函数

    转载自 http://blog.163.com/to_be_myself/blog/static/176060227201101762159227/ Matlab中用于计算自相关函数的指令是xcorr ...

  4. viso图插入Word中大片空白解决办法

    按住CTRL-->将鼠标指向绘图页边缘-->指针变为移动符号(双向箭头)-->按下左键修改绘图页大小.

  5. 【JavaEE企业应用实战学习记录】servlet3.0上传文件

    <%-- Created by IntelliJ IDEA. User: Administrator Date: 2016/10/6 Time: 14:20 To change this tem ...

  6. Linux下解决用户不能执行sudo的方法

    报错: xxx is not in the sudoers file.  This incident will be reported. Linux默认没有为当前用户开启sudo权限! $ su  $ ...

  7. Timer定时任务

    // main方法 public static void main(String[] args) { timerEnter(); } // 设定指定任务task在指定延迟delay后进行固定延迟per ...

  8. 平行四边形面积 light 1305

    double 不一定是与x y轴平平行 所以要正弦定理和余弦定理 似乎一定要printf输出 错了好几次 #include<iostream> #include<math.h> ...

  9. 线段树单点更新poj 2828

    n个人 他要插入的位置 和权值(这东西就最后输出来的) 倒的插就一定是他自己的位子 一个线段树维护一下就可以了 nlog(n) #include<stdio.h> #include< ...

  10. Windows 8.1 安装Ruby on Rails手记

    最近喜欢上了狂拽酷炫的Ruby 看了诸多文章都是Linux或Mac OS X环境配置,文章末尾还不忘把windows黑成一坨... 表示很无语.. 作为新世纪隐秘而强大的 .Net程序员 怎能脱离宇宙 ...