HDU_1430 魔板 【BFS+康托展开+置换】
一、题面
二、分析
该题与之前做的八数码不同,它是一个2*4的棋盘,并且没有空的区域。这样考虑的情况是很少的,依然结合康托展开,这时康托展开最多也只乘7的阶乘,完全可以BFS先预处理一遍。
这里需要注意,在处理的时候,仔细读题,他的二维变一维的顺序是顺时针一遍读过来的。
预处理完后,这里需要用一个小技巧,就是置换。
$$ \begin{pmatrix} 3 & 2 & 1 & 4 & 5 & 6 & 7 & 8\\1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \end{pmatrix} $$
上面使用的例子是$32145678$,然后相当于把它移到了和$12345678$一个起跑线上,这样做的好处就是我们预处理的答案能够适用所有情况。
假设目标状态是$87654321$,这样把目标状态置换成与上面对应的即可。
$$ \begin{pmatrix} 8 & 7 & 6 & 5 & 4 & 3 & 2 & 1\\8 & 7 & 6 & 5 & 4 & 1 & 2 & 3 \\ \end{pmatrix} $$
这样就可以直接输出结果了。
三、AC代码
#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstring>
#include <queue>
#include <algorithm> using namespace std; const int MAXN = ;
const int fac[] = {, , , , , , , , , }; //factorial
bool visit[MAXN];
struct Node
{
int m[];
int cat;
};
char op[] = "ABC";
string ans[MAXN]; void A(Node &t)
{
std::reverse(t.m , t.m+);
} void B(Node &t)
{
int temp = t.m[];
for(int i = ; i > ; i--)
{
t.m[i] = t.m[i-];
}
t.m[] = temp;
temp = t.m[];
for(int i = ; i < ; i++)
{
t.m[i] = t.m[i+];
}
t.m[] = temp;
} void C(Node &t)
{
int temp = t.m[];
t.m[] = t.m[];
t.m[] = t.m[];
t.m[] = t.m[];
t.m[] = temp;
} int Cantor(int s[])
{
int t, ans = ;
for(int i = ; i < ; i++)
{
t = ;
for(int j = i+; j < ; j++)
{
if(s[j] < s[i])
t++;
}
ans += t*fac[-i];
}
return ans;
} void bfs()
{
memset(visit, , sizeof(visit));
Node t;
for(int i = ; i < ; i++)
t.m[i] = i+;
t.cat = Cantor(t.m);
queue<Node> Q;
ans[t.cat] = "";
visit[t.cat] = ;
Q.push(t);
while(!Q.empty())
{
Node p = Q.front();
Q.pop();
for(int i = ; i < ; i++)
{
t = p;
switch(i)
{
case : A(t);break;
case : B(t);break;
case : C(t);break;
}
t.cat = Cantor(t.m);
if( !visit[t.cat] )
{ ans[t.cat] = ans[p.cat]+op[i];
visit[t.cat] = ;
Q.push(t);
}
}
} } int main()
{
//freopen("input.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
char s[];
int a[] = {}, b[] = {};
bfs();
while(scanf("%s", s)!=EOF)
{
for(int i = ; i < ; i++)
{
a[s[i] - ''] = i+;
}
scanf("%s", s);
for(int i = ; i < ; i++)
{
b[i] = a[s[i] - ''];
}
cout << ans[Cantor(b)] << endl;
}
return ;
}
HDU_1430 魔板 【BFS+康托展开+置换】的更多相关文章
- HDU_1430——魔板,预处理,康托展开,置换,string类的+操作
Problem Description 在魔方风靡全球之后不久,Rubik先生发明了它的简化版——魔板.魔板由8个同样大小的方块组成,每个方块颜色均不相同,可用数字1-8分别表示.任一时刻魔板的状态可 ...
- hdu.1430.魔板(bfs + 康托展开)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- hdu1430魔板(BFS+康托展开)
做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...
- HDU 1430 魔板(康托展开+BFS+预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU_1043 Eight 【逆向BFS + 康托展开 】【A* + 康托展开 】
一.题目 http://acm.hdu.edu.cn/showproblem.php?pid=1043 二.两种方法 该题很明显,是一个八数码的问题,就是9宫格,里面有一个空格,外加1~8的数字,任意 ...
- 魔板 (bfs+康托展开)
# 10027. 「一本通 1.4 例 2」魔板 [题目描述] Rubik 先生在发明了风靡全球魔方之后,又发明了它的二维版本--魔板.这是一张有 888 个大小相同的格子的魔板: 1 2 3 4 8 ...
- HDU - 1430 魔板 【BFS + 康托展开 + 哈希】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...
- HDU1430;魔板(BFS+康托展开)
传送门 题意 给出初始序列与终止序列,给出三种操作,问最少经过几次操作能使初始->终止,输出操作(字典序最小) 分析 字符串只有8个字符,使用康托展开. 1.BFS将所有序列从"123 ...
- hdu1430 魔板(康拓展开 bfs预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
随机推荐
- 在aspx页动态加载ascx页面内容,给GridView控件绑定数据
在aspx页动态加载ascx页面内容 //加载ascx页面内容Control c1 = this.Page.LoadControl("WebUserControl1.ascx"); ...
- Java 实现分页功能
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true&useUnic ...
- Linux pkg-config命令
一.简介 pkg-config用来检索系统中安装库文件的信息.典型的是用作库的编译和连接. 二.实例 http://blog.chinaunix.net/uid-20595934-id-1918368 ...
- 正确设置-Dfile.encoding参数
正确设置-Dfile.encoding参数 摘自:https://blog.csdn.net/youge/article/details/6178265 2011年02月11日 10:18:00 阅读 ...
- LightOJ 1284 Lights inside 3D Grid (数学期望)
题意:在一个三维的空间,每个点都有一盏灯,开始全是关的.现在每次随机选两个点,把两个点之间的全部点,开关都按一遍,问k次过后开着的灯的期望数量: 析:很容易知道,如果一盏灯被按了奇数次,那么它肯定是开 ...
- 编写高质量代码改善C#程序的157个建议——建议53:必要时应将不再使用的对象引用赋值为null
建议53:必要时应将不再使用的对象引用赋值为null 在CLR托管的应用程序中,存在一个“根”的概念,类型的静态字段.方法参数.以及局部变量都可以作为“根”的存在(值类型不能作为“根”,只有引用类型的 ...
- 装饰(Decorator)模式
一. 装饰(Decorator)模式 装饰(Decorator)模式又名包装(Wrapper)模式[GOF95].装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 二. 装饰模式 ...
- CentOS7 yum安装lamp环境
1.安装apache yum install httpd #根据提示,输入Y安装即可成功安装 systemctl start httpd.service #启动apache systemctl sto ...
- linux chmod对文件权限的操作
在Unix和Linux的各种操作系统下,每个文件(文件夹也被看作是文件)都按读.写.运行设定权限. 例如我用ls -l命令列文件表时,得到如下输出: -rw-r--r-- 1 apple users ...
- 【leetcode 5040. 边框着色】解题报告
方法一:dfs的非递归形式 using ll=long long; const ll MAXN=50LL; unordered_set<ll> vis,mark; vector<ve ...