【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标记.然后又交一发果断超时,伤心,最恨超时,还不如来个 ...
随机推荐
- 零元学Expression Blend 4 – Chapter 20 以实作案例学习Childwindow
原文:零元学Expression Blend 4 – Chapter 20 以实作案例学习Childwindow 本章将教大家如何运用Blend 4内建的假视窗原件-「ChildWindow」 Chi ...
- linux清除日志文件
#!/bin/bash # 清除log文件 LOG_DIR=/var/log ROOT_UID= # $UID为0的时候,用户才具有root用户的权限 LINES= # 默认的保存行数 E_XCD= ...
- [Game-0001] 新手引导逻辑梳理
欢迎任何形式的转载,但请务必注明出处:http://www.cnblogs.com/liangjingyang 欢迎任何形式的转载,但请务必注明出处:http://www.cnblogs.com/li ...
- VS2008 环境中完美搭建 Qt 4.7.4 静态编译的调试与发布(很不错,有六张插图说明,修改qmake.conf里的MD参数等)good
为什么要在VS2008中使用QT静态编译呢?很简单,因为VS2008编译器编译出来的文件比mingw编译的要几乎小一半. 好了现在我们来做些准备工作,VS2008自然要安装的,然后打上SP1的补丁.然 ...
- 在VS如何查看汇编代码
由于最近不常用,结果导致今天用的时候忘记了,╮(╯▽╰)╭.现在标记一下: 方法如下,先创建一个C++ Project,然后加入上面的代码,在main函数或者其他地方设置断点,注意是Debug版本,否 ...
- IntelliJ IDEA热部署
如何对webAPP实施热部署: 首先修改Configurations里面的 其次在设置中修改 使用debug模式运行即可
- mysql自动安装教程说明
这里只说明了思路和方法 我们在安装程序里面可能需要安装的时候将mysql一起安装,那么我们就按照下面的顺序思路来. 首先我们安装的电脑上可能已经安装了mysql,所以我们的mysql服务就起一个名字, ...
- 初探 C# 8 的 Nullable Reference Types
溫馨提醒:本文提及的 C# 8 新功能雖已通過提案,但不代表將來 C# 8 正式發布時一定會納入.這表示我這篇筆記有可能白寫了,也表示您不必急著瞭解這項新功能的所有細節,可能只要瞄一下底下的「概要」說 ...
- Realm_King 之 .NET操作XML完整类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;u ...
- 【转载】Spring Boot引起的“堆外内存泄漏”排查及经验总结
背景 为了更好地实现对项目的管理,我们将组内一个项目迁移到MDP框架(基于Spring Boot),随后我们就发现系统会频繁报出Swap区域使用量过高的异常.笔者被叫去帮忙查看原因,发现配置了4G堆内 ...