康托展开+反向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. JavaScript 替换所有匹配内容

    由于JavaScript 的 replace 只能替换一次,因此另外编写一个能现替换全部匹配内容方法,代码如下: /*把 content 中所有的 searchValue 替换为 replaceVal ...

  2. 死磕mysql(5)

    高性能mysql --查看隐藏的库//use mysql --创建新用户//create user 'new'@'localhost' identified by ''; --创建用户 '用户名'@' ...

  3. 解决Eclipse无法安装STS

    使用Eclipse Neon安装Spring Tool Suite报错: Cannot complete the install because one or more required items  ...

  4. Entity Framework 实体状态

    从今天开始我们开始讲解EF中的实体状态和数据操作,这篇文章先讲解实体状态. 我们通过前面的学习,知道EF通过上下位负责跟踪实体的状态,实体状态的位置是在命名空间 System.Dat.Entity 里 ...

  5. java8种基本数据类型

  6. 【问题】多重继承时,super函数只初始化继承的第一个类,不初始化第二个类。

    class A(object): def __init__(self): print("init class A") class B(object): def __init__(s ...

  7. Kail Linux下载与安装

    官方镜像: https://www.kali.org/downloads/ 官方虚拟机镜像: https://www.offensive-security.com/kali-linux-vm-vmwa ...

  8. 用c语言实现简单的五子棋

    用c语言实现简单的五子棋 这个小游戏是从零开始的实现的,框架灵感来自于小游戏<走迷宫>. 游戏代码配置: 二维数组+简单逻辑+getch读取键盘+windows函数(刷屏,改颜色,改窗口大 ...

  9. C语言基础二 练习

    指出正确标识符 命名 l 只能由26个英文字母的大小写.10个阿拉伯数字0~9.下划线_组成 l 严格区分大小写,比如test和Test是2个不同的标识符 l 不能以数字开头 l 不可以使用关键字作为 ...

  10. [Contract] Solidity address payable 转换与数组地址

    address payable  -->  address address payable addr1 = msg.sender; address addr2 = addr1; // 隐式转 a ...