康托展开+反向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. 如何最快实现物流即使查询功能-物流轨迹查询API

    上一篇文章我们介绍了一个物流服务提供商,推荐大家使用快递鸟接口,主要介绍了如何注册账号,获得密钥,找不到注册地址的,我在发一下: http://kdniao.com/reg 今天我们来聊如何利用快递鸟 ...

  2. LeetCode 304. Range Sum Query 2D - Immutable 二维区域和检索 - 矩阵不可变(C++/Java)

    题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

  3. MySQL8.0关系数据库基础教程(三)-select语句详解

    1 查询指定字段 在 employee 表找出所有员工的姓名.性别和电子邮箱. SELECT 表示查询,随后列出需要返回的字段,字段间逗号分隔 FROM 表示要从哪个表中进行查询 分号为语句结束符 这 ...

  4. Qt中字符串和数值之间的转换

    来自<Qt5.9 C++开发指南> 普通数值和字符串之间的转换 一.从字符串转换为数值 QString类从字符串转换为整数的函数有: int QString::toInt(bool *ok ...

  5. 【Bullet引擎】刚体类 —— btRigidBody

    btRigidBody类主要用于刚体数据的计算. 在模拟刚体动画过程中,可以使用btRigidBody类获取所保存的刚体对象,进而控制刚体对象的旋转和位移.进行刚体模拟计算需要经常用到此类. API: ...

  6. 单线程的REDIS为什么这么快?

    REDIS是单线程处理所有请求,和一般经典实际上推荐的方式相反,那么单线程串行处理,为什么依然能够做到很快呢?知乎上的一个答案如下,其中线程切换和锁不是性能主要影响因素的观点和一般的答案都不同: 作者 ...

  7. maven mvn 安装介绍

    maven是什么? Maven是基于项目对象模型(POM project object model),可以通过一小段描述信息(配置)来管理项目的构建,报告和文档的软件项目管理工具 Maven 除了以程 ...

  8. Mysql 删除从数据库的relay logs最佳方式、最安全方式

    情景 MySQL数据库主从复制在默认情况下从库的relay logs会在SQL线程执行完毕后被自动删除.但是:在relay_log_purge = 0和MHA集群下,不会被自动删除,需要手动删除.如何 ...

  9. shell脚本 定期删除日志

    定期删除日志: 然后建立清除日志文件的shell脚本,文件名为clean_log只保留最近三天的日志     #! /bin/bashlogdir=/var/log/httpdcd ${logdir} ...

  10. 关键两步+6个要点,让Windows应用程序享有K8S的绝佳优势

    本文来自Rancher Labs 前  言 实际上,没有一个迁移路径能够适用于将所有传统应用程序迁移到云.这些应用程序通常在物理机.虚拟机或本地.虽然一般情况下是重新设计应用程序架构以适用云原生服务, ...