题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=1043

题目描述:

  3*3的格子,填有1到8,8个数字,还有一个x,x可以上下左右移动,问最终能否移动到12345678x的状态?

  hint:每一个3*3的格子从上到右,从左到右,一行一行读。

解题思路:

  比较简单的八数码问题,大一暑假老师讲过,一直手懒脑懒并没有亲自尝试过。因为是多实例,先从12345678x的状态bfs出来所有可以到达的状态,并且记录下来路径。八数码最重要的就是保存状态,如果这个题目用十进制保存状态的话,至少要到达10^9的数量级。可以利用康拓展开hash搜索到的状态,空间可以缩小到9!(ง •̀_•́)ง

 #include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std; #define maxn 370000
struct node
{
string path;//路径
int s[]; //state
int loc; //9的位置
int ct; //康拓hash
};
int fac[] = {, , , , , , , *, **, ***};
//0!,1!,2!....9!
int dir[][] = {,, ,-, -,, ,}; // r, l, u, d
int vis[maxn];
char di[] = "lrdu"; //l, r, d, u
string path[maxn];
void init ()
{
memset (vis, , sizeof(vis));
} int cantor (int a[]) //康拓hash
{
int sum = ;
for (int i=; i<; i++)
{
int m = ;
for (int j=i+; j<; j++)
if (a[j] < a[i])
m ++;
sum += m * (fac[-i-]);
}
return sum + ;
} void bfs ()
{
node cur, next;
queue <node> Q; for(int i=; i<; i++)
cur.s[i] = i + ;
cur.s[] = ;
cur.ct = cantor (cur.s);
cur.loc = ; Q.push (cur);
vis[cur.ct] = ;
path[cur.ct] = ""; while (!Q.empty())
{
cur = Q.front();
Q.pop (); for (int i=; i<; i++)
{
int x = cur.loc / + dir[i][];
int y = cur.loc % + dir[i][];
if (x< || x> || y< || y>)
continue;
next = cur;
next.loc = x * + y;
next.s[cur.loc] = next.s[next.loc];
next.s[next.loc] = ;
next.ct = cantor (next.s);
if (!vis[next.ct])
{
vis[next.ct] = ;
path[next.ct] = di[i] + path[cur.ct];
Q.push (next);
}
}
} } int main ()
{
init ();
bfs ();
char s[];
int a[]; while (scanf ("%s", s) != EOF)
{
if (s[] == 'x')
a[] = ;
else
a[] = s[] - ''; for (int i=; i<; i++)
{
scanf ("%s", s);
if (s[] == 'x')
a[i] = ;
else
a[i] = s[] - '';
}
int m = cantor (a);
if (vis[m])
cout<<path[m]<<endl;
else
puts ("unsolvable");
}
return ;
}

Hdu 1043 Eight (八数码问题)的更多相关文章

  1. HDU 1043 Eight 八数码问题 A*算法(经典问题)

    HDU 1043 Eight 八数码问题(经典问题) 题意 经典问题,就不再进行解释了. 这里主要是给你一个状态,然后要你求其到达\(1,2,3,4,5,6,7,8,x\)的转移路径. 解题思路 这里 ...

  2. hdu 1043 Eight (八数码问题)【BFS】+【康拓展开】

    <题目链接> 题目大意:给出一个3×3的矩阵(包含1-8数字和一个字母x),经过一些移动格子上的数后得到连续的1-8,最后一格是x,要求最小移动步数. 解题分析:本题用BFS来寻找路径,为 ...

  3. HUD 1043 Eight 八数码问题 A*算法 1667 The Rotation Game IDA*算法

    先是这周是搜索的题,网站:http://acm.hdu.edu.cn/webcontest/contest_show.php?cid=6041 主要内容是BFS,A*,IDA*,还有一道K短路的,.. ...

  4. 【双向广搜+逆序数优化】【HDU1043】【八数码】

    HDU上的八数码 数据强的一B 首先:双向广搜 先处理正向搜索,再处理反向搜索,直至中途相遇 visit 和 队列都是独立的. 可以用一个过程来完成这2个操作,减少代码量.(一般还要个深度数组) 优化 ...

  5. hdu 1043 Eight 经典八数码问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...

  6. HDU 1043 Eight(八数码)

    HDU 1043 Eight(八数码) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Problem Descr ...

  7. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  8. HDU 1043 Eight (BFS&#183;八数码&#183;康托展开)

    题意  输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数  即这个排列在全部排列中的字典序  然后就是基础的BFS了 #include <bits/stdc++.h ...

  9. HDU 1043 八数码(A*搜索)

    在学习八数码A*搜索问题的时候须要知道下面几个点: Hash:利用康托展开进行hash 康托展开主要就是依据一个序列求这个序列是第几大的序列. A*搜索:这里的启示函数就用两点之间的曼哈顿距离进行计算 ...

随机推荐

  1. 云打印-Beta-凡事预则立

    凡事预则立 课程名称:软件工程1916|W(福州大学) 团队名称: 云打印 作业要求: 项目Beta冲刺(团队) 作业目标:Beta冲刺 团队队员 队员学号 队员姓名 个人博客地址 备注 221600 ...

  2. 使用Axis2开发WebService

    一.准备 1.下载Axis2.eclipse插件 axis2-1.6.2-war.zip: http://mirror.bjtu.edu.cn/apache//axis/axis2/java/core ...

  3. Mac OS用minikube安装单节点kubernetes

    参考 https://kubernetes.io/docs/tasks/tools/install-minikube/ https://github.com/linianhui/code/blob/m ...

  4. Model drivern

    <s:hidden name="id" value="%{role.id}"></s:hidden> 其中的value传到后台是有类型的 ...

  5. searchkick HasMany Associations | index related model fields

    Project < ActiveRecord::Base searchkick has_many :categories_has_projects has_many :categories, t ...

  6. node js 安装时选择勾上path

    勾上path则会自动配置环境变量,否则必须手动去添加nodejs的环境变量.

  7. 怎么显示隐藏Mac上的隐藏文件

    打开终端,输入:defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com.app ...

  8. (MySQL里的数据)通过Sqoop Import Hive 里 和 通过Sqoop Export Hive 里的数据到(MySQL)

    Sqoop 可以与Hive系统结合,实现数据的导入和导出,用户需要在 sqoop-env.sh 中添加HIVE_HOME的环境变量. 具体,见我的如下博客: hadoop2.6.0(单节点)下Sqoo ...

  9. ZOJ 3706 Break Standard Weight 解题报告

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5009 题目意思:给出两个mass:x 和 y,问如何将其中一个 ma ...

  10. 一步一步学Silverlight 2系列(32):图形图像综合实例—“功夫之王”剧照播放

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...