题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 , 康托展开 + BFS + 打表。

  经典八数码问题,传说此题不做人生不完整,关于八数码的八境界:http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html

  我自己是用哈希(康托展开) + BFS  + 打表过的,第三重境界。

  由于一些高级的搜索现在还没学,所以目前能升级的也就是用双向BFS来做了,等过几天有心情了来做。

  本文长期更新。


算法:  

  由于此题是求某个起始状态到“12345678X”的步骤,所以可以考虑先求出“12345678X”的所有变换的步骤,即打表预处理。

  这里要注意就是最后输出的时候,由于我们求得是逆序,所以要倒序输出,倒序输出的时候要注意'u'、'd'、'l'、'r'都应该颠倒过来,比如说你从A状态向上变换为B状态,这时你从B变为A就要向下变换。

  这里哈希还是用的康托展开,这个问题我在上一篇博客中有提到过。

BFS + 打表:

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL __int64
#define eps 1e-8
#define INF 1e8
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int MOD = ;
const int maxn = ;
bool vis[maxn];
string ans[maxn];
int fac[]={ , , , , , , , , , }; int Cantor(string str)
{
int ret = ;
int n = str.size();
for(int i = ; i < n ; i++) {
int cnt = ;
for(int j = i ; j < n ; j++)
if(str[j] < str[i])
cnt++;
ret += cnt * fac[n - i - ];
}
return ret;
}
void move_Up(string &str , int pos)
{
swap(str[pos] , str[pos - ]);
}
void move_Down(string &str , int pos)
{
swap(str[pos] , str[pos + ]);
}
void move_Left(string &str , int pos)
{
swap(str[pos] , str[pos - ]);
}
void move_Right(string &str , int pos)
{
swap(str[pos] , str[pos + ]);
}
void BFS(string start)
{
memset(vis , , sizeof(vis));
queue <string> que;
que.push(start);
int x = Cantor(start);
vis[x] = ;
ans[x] = "";
int pos;
while(!que.empty()) {
string u = que.front();
que.pop();
int i = Cantor(u);
for(pos = ; pos < ; pos++)
if(u[pos] == 'x')
break; if(pos > ) {
string tmp = u;
move_Up(tmp , pos);
int k = Cantor(tmp);
if(!vis[k]) {
vis[k] = ;
que.push(tmp);
ans[k] = ans[i] + 'u';
}
}
if(pos < ) {
string tmp = u;
move_Down(tmp , pos);
int k = Cantor(tmp);
if(!vis[k]) {
vis[k] = ;
que.push(tmp);
ans[k] = ans[i] + 'd';
}
}
if(pos % != ) {
string tmp = u;
move_Left(tmp , pos);
int k = Cantor(tmp);
if(!vis[k]) {
vis[k] = ;
que.push(tmp);
ans[k] = ans[i] + 'l';
}
}
if(pos % != ) {
string tmp = u;
move_Right(tmp , pos);
int k = Cantor(tmp);
if(!vis[k]) {
vis[k] = ;
que.push(tmp);
ans[k] = ans[i] + 'r';
}
}
}
}
int main()
{
char ch[];
string start = "12345678x";
BFS(start);
while(gets(ch))
{
string str = "";
for(int i = ; ch[i] != '\0' ; i++) {
if(ch[i] == ' ')
continue;
else
str += ch[i];
}
int k = Cantor(str);
if(vis[k]) {
for(int i = ans[k].size() - ; i >= ; i--) {
if(ans[k][i] == 'u')
putchar('d');
else if(ans[k][i] == 'd')
putchar('u');
else if(ans[k][i] == 'l')
putchar('r');
else
putchar('l');
}
puts("");
} else {
cout << "unsolvable" << endl;
}
}
return ;
}

HDU1043 八数码(BFS + 打表)的更多相关文章

  1. hdu-1043(八数码+bfs打表+康托展开)

    参考文章:https://www.cnblogs.com/Inkblots/p/4846948.html 康托展开:https://blog.csdn.net/wbin233/article/deta ...

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

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

  3. code1225 八数码Bfs

    Bfs搜索 1.把棋盘直接作为状态: #include<iostream> #include<cstring> #include<queue> #include&l ...

  4. POJ1077 八数码 BFS

    BFS 几天的超时... A*算法不会,哪天再看去了. /* 倒搜超时, 改成顺序搜超时 然后把记录路径改成只记录当前点的操作,把上次的位置记录下AC..不完整的人生啊 */ #include < ...

  5. POJ1077&&HDU1043(八数码,IDA*+曼哈顿距离)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30127   Accepted: 13108   Special ...

  6. luogu_1379 八数码难题

    八数码-->BFS+set #include<iostream> #include<cstdlib> #include<cstdio> #include< ...

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

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

  8. hdu1043 经典的八数码问题 逆向bfs打表 + 逆序数

    题意: 题意就是八数码,给了一个3 * 3 的矩阵,上面有八个数字,有一个位置是空的,每次空的位置可以和他相邻的数字换位置,给你一些起始状态 ,给了一个最终状态,让你输出怎么变换才能达到目的. 思路: ...

  9. poj 1077-Eight(八数码+逆向bfs打表)

    The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...

随机推荐

  1. IPMITOOL 配置BMC用户设置

    IPMITOOL 配置BMC用户设置 本文档共介绍5条ipmi设置user的命令,这些命令需要使用root权限才能使用,其中- H为需要操作的BMC ip,-I lanplus为使用rmcp+协议发送 ...

  2. 【C#】VS2017 winform 打包

    首先要在想要打包的项目下创建一个新的项目, 创建好setup项目,之后点击属性,去修改打包软件的名字,ProductName....可以选填 到此已经创建好了setup工程了,那么下面开始将要打包的d ...

  3. vue+element-ui 实现分页

    <el-table ref="multipleTable" :data="tableData.slice((currentPage-1)*pagesize,curr ...

  4. OpenStack 负载均衡底层命令创建流程

    1.创建负载均衡器pool-------------不支持高可用的负载均衡器 neutron lb-pool-create --lb-method ROUND_ROBIN --name mypool- ...

  5. git上传提交遇到问题

    git上传提交遇到问题 一. The local repository is out of date.Make sure all changes have been pulled from the r ...

  6. 洛谷P1962 斐波那契数列

    传送门 不难得到状态转移矩阵 然后带进去乱搞 //minamoto #include<iostream> #include<cstdio> #include<cstrin ...

  7. Jmeter-返回值乱码处理

    Jmeter安装目录/bin/jmeter.properties中sampleresult.default.encoding默认为ISO-8859-1,将参数修改为 sampleresult.defa ...

  8. EOS 多主机多节点配置终极命令

    eosio 10.186.11.211 hml 10.186.11.223 lwz 10.186.11.220 lx 10.186.11.141 //eosio private key 5K463yn ...

  9. Educational Codeforces Round 48 (Rated for Div. 2) B 1016B Segment Occurrences (前缀和)

    B. Segment Occurrences time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  10. [題解](搜索)生日蛋糕(NOI1999)

    搜索剪枝, 1.枚舉上下界: 先$R\subset$$(dep,min(\lfloor\sqrt{n-v}\rfloor,lastr-1))$ 后$H\subset$$(dep,min((n-v)/R ...