康托展开+反向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. kubernetes容器端口设置的坑

    1.使用dockerhub上面的镜像的时候,先到dockerhub上看镜像的相关信息. 2.不能随便修改容器应用的镜像,会出问题.

  2. 中国天气网API接口

    http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data/cityinfo/101010100.h ...

  3. nginx 源码编译 用OPENSSL源码 开启 SSL

    ./configure --prefix=/usr/local/nginx --with-openssl=/home/uniqs/thirdparty/openssl/openssl-1.1.1d - ...

  4. java设计模式3——建造者模式

    java设计模式3--建造者模式 1.建造者模式介绍: 建造者模式属于创建型模式,他提供了一种创建对象得最佳方式 定义: 将一个复杂对象的构建和与它的表示分离,使得同样的构建过程可以创建不同的表示 主 ...

  5. Codeforces_496_E_贪心

    http://codeforces.com/problemset/problem/496/E 这好像叫序列混合贪心,简单地讲,用歌去匹配最符合条件的人.用了multiset,重载了<,加快寻找最 ...

  6. pdo数据操作,3-4,0724

    require 'connect.php'; $linshi = $dbh->prepare('UPDATE `category` SET `name` = :name, `alias`=:al ...

  7. 通过Python包来剪枝、蒸馏DNN

    用 Distiller 压缩 PyTorch 模型 作者: PyTorch 中文网发布: 2018年7月15日 5,101阅读 0评论 近日,Intel 开源了一个用于神经网络压缩的开源 Python ...

  8. LeetCode 681. Next Closest Time 最近时刻 / LintCode 862. 下一个最近的时间 (C++/Java)

    题目: 给定一个"HH:MM"格式的时间,重复使用这些数字,返回下一个最近的时间.每个数字可以被重复使用任意次. 保证输入的时间都是有效的.例如,"01:34" ...

  9. 你知道EasyX吗

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

  10. JavaWeb前置知识 : 动态和静态的区别、两种架构、常见状态码

    JavaWeb程序设计(一) : 前置知识 1.动态网页与静态网页的区别: a.不要和是否有"动感"混为一谈. b.是否随着时间.地点.用户操作的改变而改变 (例如 : 在百度上搜 ...