【HDU - 1043】Eight(反向bfs+康托展开)
Eight
Descriptions:
简单介绍一下八数码问题:
在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图:
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 |
在上图中,由于右下角位置是空的,你可以移动数字,比如可以将数字6下移一位:
| 1 | 2 | 3 | 1 | 2 | 3 | |
| 4 | 5 | 6 | → | 4 | 5 | |
| 7 | 8 | 7 | 8 | 6 |
或者将数字8右移一位:
| 1 | 2 | 3 | 1 | 2 | 3 | |
| 4 | 5 | 6 | → | 4 | 5 | 6 |
| 7 | 8 | 7 | 8 |
1~8按顺序排列的情况称为“初始状态”(如最上方图)。“八数码问题”即是求解对于任意的布局,将其移动至“初始状态”的方法。
给定一个现有的九宫格布局,请输出将它移动至初始状态的移动方法的步骤。
Input
输入包含多组数据,处理至文件结束。每组数据占一行,包含8个数字和表示空位的‘x’,各项以空格分隔,表示给定的九宫格布局。
例如,对于九宫格
| 1 | 2 | 3 |
| 4 | 6 | |
| 7 | 5 | 8 |
输入应为:1 2 3 x 4 6 7 5 8
Output
对于每组输入数据,输出一行,即移动的步骤。向上、下、左、右移动分别用字母u、d、l、r表示;如果给定的布局无法移动至“初始 状态”,请输出unsolvable。
如果有效的移动步骤有多种,输出任意即可。
Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
题目链接
https://vjudge.net/problem/HDU-1043
其实就是反向bfs,不过用了一个新的方法去存放拼图序列,康托展开即把拼图(x12345678)全排列,再用数字去表示,简而言之就是用不同的数字去代替拼图序列,使之跟简单、快速的搜索
不会康托展开不关系,点击下面链接,现学现会
https://www.cnblogs.com/sky-stars/p/11216035.html
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 362880+5//876543210的hash值为362880 即最多出现362880种可能
using namespace std;
static const int FAC[] = {, , , , , , , , , }; // 阶乘
struct node
{
string path;//路径
int hashs;//hash值
int pos;//0的位置
};
node now,net;
queue<node>q;
int dt[][]= {{,},{-,},{,},{,-}};//四个方向
char op[]="udlr";//这个与上面的搜索方向是反的,因为是反向bfs
int tmp[];//临时存储拼图的序列
int result=;//123456780 最终答案的hash值
string path[Maxn];//path[x] hash值为x的路径
int vis[Maxn];//vis[x] hash值为x的拼图序列是否标记过
//康托展开
int cantor(int *a)
{
int x = ;
for (int i = ; i < ; ++i)
{
int smaller = ; // 在当前位之后小于其的个数
for (int j = i + ; j < ; ++j)
{
if (a[j] < a[i])
smaller++;
}
x += FAC[ - i - ] * smaller; // 康托展开累加
}
return x+; // 康托展开值
}
//逆康托展开
void decantor(int x, int *a)
{
vector<int> v; // 存放当前可选数
for(int i=; i<; i++)
v.push_back(i);
for(int i=; i<; i++)
{
int r = x % FAC[-i-];
int t = x / FAC[-i-];
x = r;
sort(v.begin(),v.end());// 从小到大排序
a[i]=v[t]; // 剩余数里第t+1个数为当前位
v.erase(v.begin()+t); // 移除选做当前位的数
}
}
void bfs()
{
MEM(vis,);//初始化
for(int i=; i<; i++)//tmp一开始为123456780,从这开始打散拼图
tmp[i]=i+;
tmp[]=;
now.pos=;
now.hashs=result;
now.path="";
path[result]="";
vis[result]=;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=; i<; i++)//四个方向
{
int tx=now.pos/+dt[i][];
int ty=now.pos%+dt[i][];
if(tx>=&&ty>=&&tx<=&&ty<=)//没走出去拼图
{
net=now;
net.pos=tx*+ty;
decantor(now.hashs-,tmp);//求tmp
swap(tmp[now.pos],tmp[net.pos]);//得到新的tmp
net.hashs=cantor(tmp);//得到新tmp对应的hash
if(!vis[net.hashs])//这都bfs老套路了 没啥说的
{
vis[net.hashs]=;
net.path=op[i]+net.path;
q.push(net);
path[net.hashs]=net.path;
}
}
}
}
return;
}
int main()
{
bfs();
char x;
while(cin>>x)//输入格式 没啥说的
{
if(x=='x')
{
now.pos=;
tmp[]=;
}
else
{
tmp[]=x-'';
}
for(int i=; i<; i++)
{
cin>>x;
if(x=='x')
{
now.pos=i;
tmp[i]=;
}
else
{
tmp[i]=x-'';
}
}
now.hashs=cantor(tmp);//求出tmp这个拼图序列的hash值
if(!vis[now.hashs])//这个hash没标记过,即没产生过这个拼图序列
cout<<"unsolvable"<<endl;
else
cout<<path[now.hashs]<<endl;//输出hash的路径
}
return ;
}
【HDU - 1043】Eight(反向bfs+康托展开)的更多相关文章
- HDU 1043 Eight(反向BFS+打表+康托展开)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...
- hdu.1430.魔板(bfs + 康托展开)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU 1043 Eight (A* + HASH + 康托展开)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- HDU_1043 Eight 【逆向BFS + 康托展开 】【A* + 康托展开 】
一.题目 http://acm.hdu.edu.cn/showproblem.php?pid=1043 二.两种方法 该题很明显,是一个八数码的问题,就是9宫格,里面有一个空格,外加1~8的数字,任意 ...
- HDU 1043 Eight(双向BFS+康托展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
- POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3
http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...
- HDU 1430 魔板(康托展开+BFS+预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU - 1430 魔板 【BFS + 康托展开 + 哈希】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...
- hdu 1430 (BFS 康托展开 或 map )
第一眼看到这题就直接BFS爆搜,第一发爆了内存,傻逼了忘标记了,然后就改,咋标记呢. 然后想到用map函数,就8!个不同的排列,换成字符串用map标记.然后又交一发果断超时,伤心,最恨超时,还不如来个 ...
随机推荐
- 微信小程序把玩(二十)slider组件
原文:微信小程序把玩(二十)slider组件 slider滑动组件用的不太多,在其他平台反正我是用的不多 主要属性: wxml <slider max="100" step= ...
- Android零基础入门第3节:带你一起来聊一聊Android开发环境
原文:Android零基础入门第3节:带你一起来聊一聊Android开发环境 工欲善其事,必先利其器.Android开发人员在自己的计算机上编写和测试应用程序,然后将其部署到实际的设备上,那首先必不可 ...
- 多态与虚拟 : 物件导向的精髓 (侯捷在石器时代对OO的理解)
[自序]虑而後能得(自序)故事接触 C++ 大约是 1989 年的事.那时候的 PC 以现在的眼光看,除了「蛮荒」之外没有更合适的形容词了.横扫千军的 Windows 3.0 还没有诞生,如今以 C+ ...
- windows下Qt5.2 for android开发环境搭建
windows下Qt5.2 forAndroid开发环境配置 1.下载安装Qt 5.2.0 for Android (Windows 32-bit) http://qt-project.org/d ...
- XPath概述
1. XPath 具体示例可参考网址: http://www.zvon.org/xxl/XPathTutorial/General/examples.html 1.1 概述 * 现节点下所有元素 * ...
- .net core 2.0 dbfirst 报 dotnet ef 未找到与命令“dotnet-ef”匹配的可执行文件的解决办法
问题描述: 最近研究了一段时间.netcore dbfirst,新建类库用来生成实体模型,执行命令总是提示 未找到与命令“dotnet-ef”匹配的可执行文件,根据网上的解决办法引用 Microsof ...
- Zookeeper详解-工作流和leader选举(三)
一.工作流 一旦ZooKeeper集合启动,它将等待客户端连接.客户端将连接到ZooKeeper集合中的一个节点.它可以是leader或follower节点.一旦客户端被连接,节点将向特定客户端分配会 ...
- python trojan development 2nd —— use python to send mail and listen to the key board then combine them
请勿用于非法用途!!!!!本人概不负责!!!原创作品,转载说明出处!!!!! from pynput.keyboard import Key,Listener import logging impor ...
- RabbitMq-安装篇
嘿,大家好,今天更新的内容是rabbitMq的安装篇~~ windows下安装rabbitMq rabbitMq下载地址:点我下载 1.由于rabbitMq用erlang语言开发,所以安装rabbit ...
- pycharm同步代码到linux(转)
pycharm是一个非常强大的python开发工具,现在很多代码最终在线上跑的环境都是linux,而开发环境可能还是windows下开发,这就需要经常在linux上进行调试,或者在linux对代码进行 ...