题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043

  八数码问题,因为固定了位置所以以目标位置开始搜索,把所有情况(相当于一个排列)都记录下来,用康托展开来完成序列的记录问题开始BFS。打好表后按给出序列的康托数确定开始位置,逆向查找。

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; typedef struct Node1 {
char s;
int pre;
Node1() { pre = -; }
}Node1;
typedef struct Node2 {
int status[];
int n, son;
}Node2; Node1 path[];
int dx[] = {, -, , };
int dy[] = {, , , -};
int fac[]; void init() {
fac[] = fac[] = ;
for(int i = ; i <= ; i++) {
fac[i] = fac[i-] * i;
}
} int ecantor(int* s, int n = ) {
int num = ;
for(int i = ; i < n; i++) {
int tmp = ;
for(int j = i + ; j < n; j++) {
if(s[j] < s[i]) {
tmp++;
}
}
num += fac[n--i] * tmp;
}
return num;
} void bfs() {
queue<Node2> q;
Node2 a, b;
int t = ;
for(int i = ; i < ; i++) {
a.status[i] = i + ;
}
a.n = ; a.son = ;
path[a.son].pre = ;
q.push(a);
while(!q.empty()) {
a = q.front(); q.pop();
for(int i = ; i < ; i++) {
b = a;
int xx = a.n % + dx[i];
int yy = a.n / + dy[i];
if(!(xx >= && xx < && yy >= && yy < )) continue;
b.n = yy * + xx;
swap(b.status[b.n], b.status[a.n]);
b.son = ecantor(b.status);
if(path[b.son].pre == -) {
path[b.son].pre = a.son;
if(i == ) path[b.son].s = 'l';
if(i == ) path[b.son].s = 'r';
if(i == ) path[b.son].s = 'u';
if(i == ) path[b.son].s = 'd';
q.push(b);
}
}
}
} int main() {
// freopen("in", "r", stdin);
init();
int s, ss[];
char ch[];
bfs();
while(gets(ch)) {
int cnt = ;
for(int i = ; ch[i]; i++) {
if(ch[i] == 'x') {
ss[cnt++] = ;
}
else if(ch[i] >= '' && ch[i] <= '') {
ss[cnt++] = ch[i] - '';
}
}
s = ecantor(ss);
if(path[s].pre == -) {
printf("unsolvable\n");
continue;
}
while(s != ) {
printf("%c", path[s].s);
s = path[s].pre;
}
printf("\n");
}
return ;
}

[HDOJ1043]Eight(康托展开 BFS 打表)的更多相关文章

  1. HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3

    http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过, ...

  2. HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  3. HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  4. HDU 1430 魔板(康托展开+BFS+预处理)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  5. hdu1043Eight (经典的八数码)(康托展开+BFS)

    建议先学会用康托展开:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description The 15-puzzle ...

  6. poj1077(康托展开+bfs+记忆路径)

    题意:就是说,给出一个三行三列的数组,其中元素为1--8和x,例如: 1 2 3 现在,需要你把它变成:1 2 3 要的最少步数的移动方案.可以右移r,左移l,上移u,下移dx 4 6 4 5 67 ...

  7. HDU1043 八数码(BFS + 打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 , 康托展开 + BFS + 打表. 经典八数码问题,传说此题不做人生不完整,关于八数码的八境界 ...

  8. 转换地图 (康托展开+预处理+BFS)

    Problem Description 在小白成功的通过了第一轮面试后,他来到了第二轮面试.面试的题目有点难度了,为了考核你的思维能量,面试官给你一副(2x4)的初态地图,然后在给你一副(2x4)的终 ...

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

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

随机推荐

  1. 【BZOJ】【4010】【HNOI2015】菜肴制作

    拓扑排序 这题是要求N个点的一个拓扑序,且满足以下条件:编号1的位置尽可能靠前,在此基础上编号2的位置尽可能靠前…… 我看到这题的第一感觉:将拓扑排序用的队列改为优先队列,编号越小越早出来. 但是连样 ...

  2. HDOJ 1709 The Balance(母函数)

    The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. PHP合并数组array_merge函数运算符加号与的区别

    两个的区别是:1.数组键名为数字键名时,要合并的两个数组中有同名数字KEY的时候,使用array_merge()不会覆盖掉原来的值,而使用“+”合并数组则会把最先出现的值作为最终结果返回,而把后面的数 ...

  4. TKStudio 4.6IDE Warning: L6310W: Unable to find ARM libraries.

    我也遇到了同样的问题.搞了很久,按下面的操解决了 内容转至:http://bbs.zlgmcu.com/dv_rss.asp?s=xh&boardid=43&id=23032& ...

  5. 机器学习在 IT 运维管理中的必要性!

    机器学习技术在监控工具中的应用已经成为 IT 运维与 DevOps 团队的一大热点话题.尽管相关的使用案例很多,对 IT 团队而已真正的「杀手级应用」是机器学习如何提高实时事件管理能力,从而帮助较大规 ...

  6. HDU 4034 Graph(floyd,最短路,简单)

    题目 一道简单的倒着的floyd. 具体可看代码,代码可简化,你有兴趣可以简化一下,就是把那个Dijsktra所实现的功能放到倒着的floyd里面去. #include<stdio.h> ...

  7. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  8. iOS富文本-NSAttributedString简单封装

    直接调用系统的写起来比较麻烦,封装一下 因为要简单所以就写类方法 WJAttributeStyle 基类 ) {         ; i < styles.count; i ++) {      ...

  9. ListView的item选中效果

    有时app会需要点击某个item并实现选中的效果,例如做pad时用Fragment实现的左侧列表右侧内容的效果,点击左侧某一个item后会高亮选中 有时简单的使用setSelected(boolean ...

  10. WCF分布式开发步步为赢(4):WCF服务可靠性传输配置与编程开发

    今天继续WCF分布式开发步步为赢系列的第4节:WCF服务可靠性传输配置与编程开发.这个章节,我们要介绍什么是WCF服务的可靠性传输,随便介绍网络协议的概念,Web Service为什么不支持可靠性传出 ...