康托展开+反向bfs

hdu 1043

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

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct Step_s{
int parent;
char dir;
}step_set[500010];
typedef struct Node_s{
int board[10];
int x_index;
int child;
}Node;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int fac[10];
void set_fac()
{
fac[0] = 1;
for(int i=1;i<=8;i++)
{
fac[i] = fac[i-1]*i;
}
}
int board_hash(int board[])
{
int ans = 0;
for(int i=0;i<9;i++)
{
int k = 0;
for(int j=i+1;j<9;j++)
{
if(board[j]<board[i])
k++;
}
ans += k * fac[8-i];
}
return ans;
}
void bfs(int finish[])
{
queue<Node> Q;
Node current;
int tx,ty,temp;
for(int i=0;i<9;i++)
{
current.board[i] = finish[i];
}
current.x_index = 8,current.child = 0;
step_set[current.child].parent = 0;
Q.push(current);
while(Q.size())
{
current = Q.front();
Q.pop(); for(int i=0;i<4;i++)
{
Node Next = current;
tx = current.x_index % 3 + dir[i][0];
ty = current.x_index / 3 + dir[i][1];
if(tx>=0&&ty>=0&&tx<3&&ty<3)
{
Next.x_index = ty * 3 + tx;
temp = Next.board[Next.x_index];
Next.board[Next.x_index] = Next.board[current.x_index];
Next.board[current.x_index] = temp;
Next.child = board_hash(Next.board);
if(step_set[Next.child].parent == -1)
{
step_set[Next.child].parent = current.child;
if(i == 0)step_set[Next.child].dir = 'l';
if(i == 1)step_set[Next.child].dir = 'r';
if(i == 2)step_set[Next.child].dir = 'u';
if(i == 3)step_set[Next.child].dir = 'd';
Q.push(Next);
}
}
}
}
}
int main() {
int i,j,finish[10],board_input[10];
string ch;
set_fac();
for(i=0;i<9;i++)
{
finish[i] = i + 1;
}
for(i=0;i<500000;i++)
{
step_set[i].parent = -1;
} bfs(finish);
while(getline(cin,ch))
{
int s;
for(i=0,j=0;i<ch.size();i++)
{
if(ch[i] == 'x')
{
board_input[j++] = 9;
}
else if(ch[i] >= '1' && ch[i] <= '8')
{
board_input[j++] = ch[i] - '0';
}
} s = board_hash(board_input);
if(step_set[s].parent== -1 )
{
printf("unsolvable\n");
continue;
}
while(s!=0)
{
printf("%c",step_set[s].dir);
s = step_set[s].parent;
}
puts("");
}
return 0;
}

康托展开+反向bfs的更多相关文章

  1. UESTC 485 Game(康托展开,bfs打表)

    Game Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Status t ...

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

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

  3. HDU1430 BFS + 打表 + 康托展开

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1430 , 一道比较好的题. 这道题要用到很多知识,康托展开.BFS.打表的预处理还要用到一一映射,做完 ...

  4. HDU 1043 Eight(反向BFS+打表+康托展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...

  5. 【HDU - 1043】Eight(反向bfs+康托展开)

    Eight Descriptions: 简单介绍一下八数码问题:在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图: 1 2 3 4 5 6 7 8   在上图中,由于右下角位置是空的 ...

  6. 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开

    [kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...

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

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

  8. loj 1165(bfs+康托展开)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...

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

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

随机推荐

  1. 视觉slam十四讲课后习题ch3--5题

    题目回顾: 假设有一个大的Eigen矩阵,我想把它的左上角3x3块提取出来,然后赋值为I3x3.编程实现.解:提取大矩阵左上角3x3矩阵,有两种方式: 1.直接从0-2循环遍历大矩阵的前三行和三列 2 ...

  2. python学习方法

    python学习体系 python相关书籍若干本 了解python基础数据类型 熟悉各种类型的操作方法 理解函数与类的概念 练习练习再练习 前期以被动学习为主,把每个知识点都认真的学过去,后期慢慢再把 ...

  3. 深度学习中的特征(feature)指的是什么?

    一般在machine learning意义上,我们常说的feature,是一种对数据的表达.当然,要衡量一种feature是否是合适的表达,要根据数据,应用,ML的模型,方法....很多方面来看.一般 ...

  4. C++ STL——优先队列的结构体表示方法

    优先队列是队列的一种,但是自身具有一定的排序功能,所以不具有队列“先进先出”的性质 刚刚接触优先队列,看过网上的用法后感觉还是太过于朦胧,所以打算自己写一个稍微细节一点的. 头文件 #include& ...

  5. jQuery 基础 (笔记源于runoob)

    您需要具备的基础知识 在您开始学习 jQuery 之前,您应该对以下知识有基本的了解: HTML CSS JavaScript jQuery ? jQuery是一个JavaScript函数库. jQu ...

  6. BeautifulSoup入门

    BeautifulSoup库入门 BeautifulSoup库的理解 BeautifulSoup库是解析.遍历.维护”标签树”的功能库 示例代码: from bs4 import BeautifulS ...

  7. Golang-执行go get私有库提示”410 Gone“ 解决办法

    之前没有使用过私有库,今天正好碰到,把解决方法整理一下 错误记录 如果你在用go get 私有库碰到如下类似错误: 解决方法 $ export GO111MODULE=on $ export GOPR ...

  8. 后台+下载(wget)+多个下载url

    有时候我们需要让linux下载多个文件,并且后台下载,不去占用当前控制台. 比如我们需要下载中文维基的三个文件: https://dumps.wikimedia.org/zhwiki/latest/z ...

  9. aws 社交媒体技术大会 部分总结

    早上会议总结:. 1. 介绍了aws的应用,无技术知识点.略 2. 云原生:  就是一种在云中原生的一种技术 新常态:已经成为一种常态,基本都有的服务都可以部署在云端. 对 “互联网+” 和 “+互联 ...

  10. linux 下生成随机密码生成器

    [root@localhost ~]# yum -y install pwgen [root@localhost ~]# pwgen -ncCyB1 8 1 kei%b3Xa [root@localh ...