HDU1430 BFS + 打表 + 康托展开
题目链接: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 + 打表 + 康托展开的更多相关文章
- HDU1043 Eight(八数码:逆向BFS打表+康托展开)题解
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- hdu-1043(八数码+bfs打表+康托展开)
参考文章:https://www.cnblogs.com/Inkblots/p/4846948.html 康托展开:https://blog.csdn.net/wbin233/article/deta ...
- HDU 1043 Eight(反向BFS+打表+康托展开)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...
- HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3
http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过, ...
- hdoj1043 Eight(逆向BFS+打表+康拓展开)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 思路: 由于自己对康拓展开用的太少,看到这个题没想到康拓展开,最开始打算直接转换为数字,但太占内 ...
- [HDOJ1043]Eight(康托展开 BFS 打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 八数码问题,因为固定了位置所以以目标位置开始搜索,把所有情况(相当于一个排列)都记录下来,用康托 ...
- UESTC 485 Game(康托展开,bfs打表)
Game Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Status t ...
- hdu1430魔板(BFS+康托展开)
做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...
- HDU1430;魔板(BFS+康托展开)
传送门 题意 给出初始序列与终止序列,给出三种操作,问最少经过几次操作能使初始->终止,输出操作(字典序最小) 分析 字符串只有8个字符,使用康托展开. 1.BFS将所有序列从"123 ...
随机推荐
- Python开发【第三篇】:分支循环
1. if 条件语句 语法: if 条件: 代码块 # 条件为真执行 else: # else 可选 代码块 # 条件为假执行 示例: n = int(input('请输入一个数字:')) i ...
- SCUT - 223 - Maya - 构造
https://scut.online/p/223 给定两个数N,M,构造M个在[0,80000]以内的互不相同的数使之异或和为N. 首先特判一下M<=2的两个简单情况,还有坑爹的-1! 然后想 ...
- 什么时候要重写equals
什么时候要重写equals 当对象需要根据值去比较它们是否相等时,需要我们重写equals,而它的hashCode也同时需要被重要,一般来说就是对类里所有成员变更求hashCode. 没有重写equa ...
- shell脚本——循环和函数
1.打印一个等腰三角形 ` ;do -$i]` ;do echo -n ' ' done -]` ;do echo -n '*' done echo done 2.打印99乘法表 #!/bin/bas ...
- HDU1425 A Chess Game
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1524 思路:题目就是给你一个拓扑图,然后指定点的位置放棋子,然后两人轮流移动棋子(题目中的边的关系),直到 ...
- return this链式操作
function Fn(){}; Fn.prototype = { constructor:Fn, a:function(){ alert(1); return this; //实现链式操作.即fn. ...
- js抽奖系统
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Migration-添加表
public partial class _111111 : DbMigration { public override void Up() { CreateTable( "dbo.Asse ...
- 洛谷P3195||bzoj1010 [HNOI2008]玩具装箱TOY
洛谷P3195 bzoj1010 设s数组为C的前缀和 首先$ans_i=min_{j<i}\{ans_j+(i-j-1+s_i-s_j-L)^2\}$ (斜率优化dp)参考(复读)https: ...
- 客户端发送http
package com.scok; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStr ...