一、题面

POJ1430

二、分析

该题与之前做的八数码不同,它是一个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+康托展开+置换】的更多相关文章

  1. HDU_1430——魔板,预处理,康托展开,置换,string类的+操作

    Problem Description 在魔方风靡全球之后不久,Rubik先生发明了它的简化版——魔板.魔板由8个同样大小的方块组成,每个方块颜色均不相同,可用数字1-8分别表示.任一时刻魔板的状态可 ...

  2. hdu.1430.魔板(bfs + 康托展开)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  3. hdu1430魔板(BFS+康托展开)

    做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...

  4. HDU 1430 魔板(康托展开+BFS+预处理)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  5. HDU_1043 Eight 【逆向BFS + 康托展开 】【A* + 康托展开 】

    一.题目 http://acm.hdu.edu.cn/showproblem.php?pid=1043 二.两种方法 该题很明显,是一个八数码的问题,就是9宫格,里面有一个空格,外加1~8的数字,任意 ...

  6. 魔板 (bfs+康托展开)

    # 10027. 「一本通 1.4 例 2」魔板 [题目描述] Rubik 先生在发明了风靡全球魔方之后,又发明了它的二维版本--魔板.这是一张有 888 个大小相同的格子的魔板: 1 2 3 4 8 ...

  7. HDU - 1430 魔板 【BFS + 康托展开 + 哈希】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...

  8. HDU1430;魔板(BFS+康托展开)

    传送门 题意 给出初始序列与终止序列,给出三种操作,问最少经过几次操作能使初始->终止,输出操作(字典序最小) 分析 字符串只有8个字符,使用康托展开. 1.BFS将所有序列从"123 ...

  9. hdu1430 魔板(康拓展开 bfs预处理)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. docker问题:docker端口映射错误

    1 docker端口映射错误 1.1 问题描述 利用docker启动nginx容器的时候报错: 1.2 解决办法 一次执行下面的命令就可以解决 pkill docker iptables -t nat ...

  2. 1-在eclipse里面配置python(最详细)

    最近有时间打算学下python,打算学当然是得先搞好开发工具,网上搜一波,发现许多ide,居然可以在eclipse下写python,由于最近一直在搞java,所以已经装了eclipse,所以打算就在e ...

  3. 529A And Yet Another Bracket Sequence

    传送门 题目大意 给定有一个长度为n n的括号序列,现在有两种操作: 在任意一个位置插入有一个左括号或右括号 将末尾的一个括号放到最前面 可以对这个序列进行若干次操作,问在使括号序列合法的前提下,长度 ...

  4. 清除Vs2010的工作区影射关系的缓存信息的文件夹路径

    C:/Users/Administrator/AppData/Local/Microsoft/Team Foundation/3.0/Cache

  5. plsql中的执行体

    在plsql中的sql windows窗口中,可以编写一段执行体来达到一定的目的,类似于写一段程序,可有逻辑判断. 大概的格式为 declare ----定义变量 begin ----- 执行体: e ...

  6. What is difference between 3-layer architecture and MVC architecture?

    By Vikas Singh on Sep 26, 2014 In 3-layer architecture  3-layer architecture separates the applicati ...

  7. HTML5之:link与title的区别

    [link]标签:外联导入样式 例1:<link rel="stylesheet" type="text/css" href="theme.cs ...

  8. 软件工程实践一 —— java之wc.exe

    SoftwareEngineering-wc github项目地址:https://github.com/CuiLam/SoftwareEngineering-wc   项目相关要求 实现一个统计程序 ...

  9. Nginx禁止直接通过IP地址访问网站

    介绍下在nginx服务器禁止直接通过IP地址访问网站的方法,以避免别人恶意指向自己的IP,有需要的朋友参考下. 有时会遇到很多的恶意IP攻击,在Nginx下可以禁止IP访问. Nginx的默认虚拟主机 ...

  10. delphi监控文件夹

    (****************************************** 文件和目录监控 当磁盘上有文件或目录操作时,产生事件 使用方法: 开始监控: PathWatch(Self.Ha ...