康托展开+反向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. tmobst6an

    (单选题)Oracle数据库中,在SQL语句中连接字符串的方法是:(). A)CAT B)CONCAT C)JOIN D)UNION 解析:Oracle可以使用CONCAT关键字或"||&q ...

  2. shell命令之一天一见:grep

    一. 简介 grep (缩写来自Globally search a Regular Expression and Print)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来 ...

  3. HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  4. 我的一个配置redux(实现一次存储与调用方法)之旅

    前言 : 今天呢,就配置一下redux,redux的重要性呢,就叭叭叭一大堆,什么也没有带着配置一次来的重要,因为许多涉及到的属性和方法,用法是活的,但格式是需要记忆的. 过程中不要嫌我唠叨,有的地方 ...

  5. 你知道EasyX吗

    关于C++控制台上的图形界面,你知道一个东西叫做EasyX吗? 作为一个退役的OI选手(WTF?\(3^2-3^1\)年级退役?),在退役之后总想找点事情做. 开发个游戏?(我只会控制台) 学下人工智 ...

  6. windows、linux 下启用mysql日志功能

    在默认情况下,mysql安装是没有启用日志管理功能的,这为后续的维护带来很多不便的地方. 查看是否启用了日志mysql>show variables like 'log_bin'; 怎样知道当前 ...

  7. k8s系列---service

    来源 : http://blog.itpub.net/28916011/viewspace-2214745/ service是要通过coreDNS来管理pod的. kube-proxy始终监视着api ...

  8. OSPF理论

    OSPF简介 OSPF(Open Shortest Path First 开放式最短路径优先)协议是IETF为IP网络开发的IGP路由选择协议.它是一种典型的链路状态(link-state)路由协议. ...

  9. ssh_key认证

    ssh认证流程步骤: 1.主机host_key认证 2.身份验证 3.身份验证通过 原理及更多知识点,请查看好友博客 http://www.cnblogs.com/f-ck-need-u/p/7129 ...

  10. 在虚拟机中使用NetToPLCSim和PLC相连.

    1,虚拟机...系统Win10...里面安装了VS. 2,本机...系统Win10...里面安装了博图15. 3,转换软件:NetToPLCSIM. 4,本机和虚拟机连接同一个路由器.注意: 5,设置 ...