题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1430 , 一道比较好的题。

  这道题要用到很多知识,康托展开、BFS、打表的预处理还要用到一一映射,做完受益匪浅。

  其实这道题也可以用双向BFS来写,思路也已经有了,过几天再来写。

  本文持续更新。


先说搜索部分:

  对于魔板的每一个状态,都可以进行A、B、C三种操作,所以按照图论来讲,就是操作前的状态可以到达操作后的状态,所以就这样转换成了广搜问题。

  这里要注意一点,由于题目要求字典序最小的,所以搜索的时候要按照ABC的顺序来。

再说康托展开:

  康托展开其实就是哈希的一种,即用一个数字来表示一个排列。比如说{A , B , C , D}的全排列中,ABCD对应的康托展开的值为0,ABDC对应的康托展开值为1...

  关于康托展开算法,具体见:http://blog.csdn.net/zhongkeli/article/details/6966805

关于打表预处理:

  由于魔板的所有状态都可以转换为“12345678”,所以这时就需要做一个映射:每组数据都有一个起始状态与目标状态,可以把起始状态用一种映射关系映射为“12345678”,然后用这种映射关系再去改一下终止状态。例如:初态为“12653487” , 目态为“12345678” ;这时映射后初态为“12345678”,即f[1] = 1 , f[2] = 2 , f[6] = 3 , f[5] = 4 , f[3] = 5 , f[4] = 6 , f[8] = 7 , f[7] = 8 ,按照这种映射关系目态应为“12564387”。代码应为:f[start[i] - '0'] = i ; end[i] = f[end[i] - '0'] + '0';

  有这样一个映射前提,就可以先用BFS预处理从“12345678”到其余所有状态的步骤,然后输入每组测试数据后进行转换,然后这时候就变成了求从“12345678”到映射后的目标状态的步骤的问题,这时按照存好的路径输出即可。

BFS + 打表:

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL __int64
#define eps 1e-8
#define INF 1e8
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int MOD = ;
const int maxn = + ;
int vis[maxn];
string ans[maxn]; int fac[]={ , , , , , , , , };
int Cantor(string str)
{
int ret = ;
int n = str.size();
for(int i = ; i < n ; i++) {
int cnt = ;
for(int j = i ; j < n ; j++)
if(str[j] < str[i])
cnt++;
ret += cnt * fac[n - i - ];
}
return ret;
}
void move_A(string &str)
{
for(int i = ; i < ; i++)
swap(str[i] , str[ - i]);
}
void move_B(string &str)
{
for(int i = ; i > ; i--)
swap(str[i] , str[i - ]);
for(int i = ; i < ; i++)
swap(str[i] , str[i + ]);
}
void move_C(string &str)
{
char tmp = str[];
str[] = str[];
str[] = str[];
str[] = str[];
str[] = tmp;
}
void BFS(string str)
{
memset(vis , , sizeof(vis));
queue <string> que;
que.push(str);
int x = Cantor(str);
vis[x] = ;
ans[x] = "";
while(!que.empty()) {
string u = que.front();
que.pop();
int i = Cantor(u); string tmp = u;
move_A(tmp);
int k = Cantor(tmp);
if(!vis[k]) {
vis[k] = ;
que.push(tmp);
ans[k] = ans[i] + 'A';
} tmp = u;
move_B(tmp);
k = Cantor(tmp);
if(!vis[k]) {
vis[k] = ;
que.push(tmp);
ans[k] = ans[i] + 'B';
} tmp = u;
move_C(tmp);
k = Cantor(tmp);
if(!vis[k]) {
vis[k] = ;
que.push(tmp);
ans[k] = ans[i] + 'C';
}
}
}
int main()
{
int a[];
string s , e;
string start = ("");
BFS(start);
while(cin >> s >> e)
{
for(int i = ; i < ; i++)
a[s[i] - ''] = i + ;
for(int i = ; i < ; i++)
e[i] = a[e[i] - ''] + '';
int k = Cantor(e);
cout << ans[k] << endl;
}
return ;
}

  

HDU1430 BFS + 打表 + 康托展开的更多相关文章

  1. HDU1043 Eight(八数码:逆向BFS打表+康托展开)题解

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  2. hdu-1043(八数码+bfs打表+康托展开)

    参考文章:https://www.cnblogs.com/Inkblots/p/4846948.html 康托展开:https://blog.csdn.net/wbin233/article/deta ...

  3. HDU 1043 Eight(反向BFS+打表+康托展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...

  4. HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3

    http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过, ...

  5. hdoj1043 Eight(逆向BFS+打表+康拓展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 思路: 由于自己对康拓展开用的太少,看到这个题没想到康拓展开,最开始打算直接转换为数字,但太占内 ...

  6. [HDOJ1043]Eight(康托展开 BFS 打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 八数码问题,因为固定了位置所以以目标位置开始搜索,把所有情况(相当于一个排列)都记录下来,用康托 ...

  7. UESTC 485 Game(康托展开,bfs打表)

    Game Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Status t ...

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

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

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

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

随机推荐

  1. spark svm

    首先spark上的svm只能处理线性的,不能处理非线性的.其次spark上的svm求解过程与普通的不同.普通的是通过拉格朗日对偶,然后通过SMO方法求. 但是在spark上,则没有通过拉格朗日,而是直 ...

  2. SQL Server 练习

    use master if exists(select * from sys.databases where name='db_Test') drop database db_Test go crea ...

  3. Codeforces - 77B - Falling Anvils - 几何概型

    https://codeforc.es/contest/77/problem/B 用求根公式得到: \(p-4q\geq0\) 换成熟悉的元: \(y-4x\geq0\) 其中: \(x:[-b,b] ...

  4. UPC11073(DP,思维)

    #include<bits/stdc++.h>using namespace std;long long dp[507][507];const long long mod = 998244 ...

  5. 定时器详解和应用、js加载阻塞、css加载阻塞

    1.setTimeout().setInterval()详解和应用 1.1 详解: setTimeout.setInterval执行时机 1.2 存在问题: setInterval重复定时器可能存在的 ...

  6. validate验证注册表单

    点击预览; <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...

  7. UML——初识

    初识 刚刚接触到UML的时候,先看的书,对整本书的内容做了宏观的把控.感觉UML这个东西和自己想象中的不一样.起初我认为它只是一个工具,将软件开发过程中不同的阶段用不用种类的图表现出来,后来才发现它是 ...

  8. 2017-10-5 清北刷题冲刺班p.m

    套路(拓扑排序) /* 对每个联通块单独考虑. 每个联通块是一个环套树,树边拎出来可以随意定向,记树边为 m,所以树的方案数为2^m . 对于环来说只有两种方向,顺时针和逆时针,记环边为 n,所以环的 ...

  9. 洛谷P3527 [POI2011]MET-Meteors(整体二分)

    传送门 整体二分 先二分一个答案,判断是否可行,把可行的全都扔到左边,不可行的扔到右边 判断是否可行用树状数组就行 具体细节看代码好了 整体二分细节真多……也可能是我大脑已经退化了? //minamo ...

  10. springBoot2.0 配置shiro实现权限管理

    一.前言 基于上一篇springBoot2.0 配置 mybatis+mybatisPlus+redis 这一篇加入shiro实现权限管理 二.shiro介绍 2.1 功能特点 Shiro 包含 10 ...