题目链接: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. 利用PE数据目录的导入表获取函数名及其地址

    PE文件是以64字节的DOS文件头开始的(IMAGE_DOS_HEADER),接着是一段小DOS程序,然后是248字节的 NT文件头(IMAGE_NT_HEADERS),NT的文件头位置由IMAGE_ ...

  2. AngularJs学习笔记--expression

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/expression 表达式(Expressions)是类Javascript的代码片段,通常放置在绑定 ...

  3. CSS3实现的渐变按钮,在IE7、IE6下的滤镜使用。

      .ui-button { BORDER-LEFT-WIDTH:; ; COLOR: #fff; PADDING-BOTTOM: 0px; TEXT-ALIGN: center; PADDING-T ...

  4. javascript中关于坐标 大小 的描述

    window对象 有效桌面的大小,除去桌面下面的任务栏的高度 window.screen.availHeight : window.screen.availWidth :   浏览器窗口的左上角相对于 ...

  5. 全7 天玩转 ASP.NET MVC — 第 2 天

    0. 前言 我相信在开始第 2 天的学习时,你已经顺利地完成了第 1 天的课程. 我们回顾一下第 1 天的主要关注点: 为什么选择 ASP.NET MVC ? ASP.NET Webforms 和 A ...

  6. 从底层理解Python的执行

    摘要:是否想在Python解释器的内部晃悠一圈?是不是想实现一个Python代码执行的追踪器?没有基础?不要怕,这篇文章让你初窥Python底层的奥妙. [编者按]下面博文将带你创建一个字节码级别的追 ...

  7. ZOJ3717 Balloon(2-SAT)

    一个很玄乎的问题,但听到2-SAT之后就豁然开朗了.题目的意思是这样的,给你n个点群,每个点群里面有两个点,你要在每个点群里面选一个点,以这些点做半径为r的圆,然后r会有一个最大值,问的就是怎么选这些 ...

  8. hdu 1250 Hat's Fibonacci(java,简单,大数)

    题目 java做大数的题,真的是神器,来一道,秒一道~~~ import java.io.*; import java.util.*; import java.math.*; public class ...

  9. Unix安装BerkeleyDB

    下载安装包Berkeley DB 5.3.21.tar.gz http://www.oracle.com/technetwork/products/berkeleydb/downloads/index ...

  10. C# 枚举类型操作

    //定义一个枚举 public enum Colors { Red, Green, Blue, Yellow }; //根据key找value string name = Enum.GetName(t ...