题目链接: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. Leetcode#87 Scramble String

    原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...

  2. NYOJ-205 求余数 AC 分类: NYOJ 2014-02-02 12:30 201人阅读 评论(0) 收藏

    这题目看一眼以为难度评级出错了,只是一个求余数的题目,,后来才发现,位数小于百万位,,,我还以为是大小小于百万呢,所以借鉴了另一大神的代码, 用大数,重点是同余定理: (a+b)mod m=((a m ...

  3. remoting技术

    转: http://www.cnblogs.com/rickie/category/5082.html

  4. GA项目体会

    1.NaN表示运算的结果是未定义的计算过程,例如0/0.在计算EBO的时候,由于使用泊松分布的计算过程,出现了0/0的情况,所以控制台才会提示"非数字". 2.保障资金太小的时候可 ...

  5. Webpack教程二

    Webpack教程一 开发技巧 启用source-map 现在的代码是合并以后的代码,不利于排错和定位,只需要在config中添加 ... devtool: 'eval-source-map', .. ...

  6. C# WinForm窗口最小化到系统托盘

    * C# WinForm窗口最小化到系统托盘http://hi.baidu.com/kfxtgtqyapouyze/item/8ccfdcd5a174a7312a35c7c3 主要功能:(1).程序启 ...

  7. mysql之游标

    游标 行,也不存在每次一行地处理所有行的简单方法(相对于成批地处理它们).有时,需要在检索出来的行中前进或后退一行或多行.这就是使用游标的原因.游标( cursor)是一个存储在MySQL服务器上的数 ...

  8. Struts2 Convention插件的使用(2)return视图以及jsp的关系

    package com.hyy.action; import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extend ...

  9. lintcode: 最小调整代价

    题目 最小调整代价 给一个整数数组,调整每个数的大小,使得相邻的两个数的差小于一个给定的整数target,调整每个数的代价为调整前后的差的绝对值,求调整代价之和最小是多少. 样例 对于数组,最小的调整 ...

  10. 540C: Ice Cave

    题目链接 题意: n*m的地图,'X'表示有裂痕的冰块,'.'表示完整的冰块,有裂痕的冰块再被踩一次就会碎掉,完整的冰块被踩一次会变成有裂痕的冰块, 现在告诉起点和终点,问从起点能否走到终点并且使终点 ...