HDU - 1430 魔板 【BFS + 康托展开 + 哈希】
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1430
思路
我刚开始 想到的 就是 康托展开
但是这个题目是 多组输入 即使用 康托展开 也是会T的
正解应该是 预处理
然后我想到的预处理
因为每个状态 都是能够扩展出三种状态的
也就是说 每个状态都可以有三个儿子 这样 就像一棵树
我先把这棵树 建好 然后 询问的时候 再BFS搜索 这样就不用每次都康托展开了
但是还是T了
后来查了题解
发现 其实每次变换的都是位置,而上面的数字 是什么 其实不重要 我们可以给它哈希成 12345678
比如说 给出的 初始状态是 8 2 7 5 4 6 3 1 目标状态是 1 7 2 4 5 3 6 8
那么 我们可以理解为
8-1
2-2
7-3
5-4
4-5
6-6
3-7
1-8
那么 目标状态 通过哈希 就变为
8 3 2 5 4 7 6 1
问题就转化为
从 1 2 3 4 5 6 7 8 转移到 8 3 2 5 4 7 6 1 需要多少步骤
那么我们就可以 从 1 2 3 4 5 6 7 8 到达任意一种状态都预处理
这样 就可以直接输出路径了。。
我是这样处理的
保存父节点 递归输出
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
#define stack_expand #pragma comment(linker, "/STACK:102400000,102400000")
#define syn_close ios::sync_with_stdio(false);cin.tie(0);
//#define bug
//#define gets gets_s
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 4e4 + 1e3 + 10;
const int MOD = 142857;
const int FAC[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };
int cantor(int b[8])
{
int ans = 0;
for (int i = 0; i < 8; i++)
{
int smaller = 0;
for (int j = i + 1; j < 8; j++)
{
if (b[i] > b[j])
smaller++;
}
ans += FAC[7 - i] * smaller;
}
return ans;
}
int arr[8];
pii pre[maxn];
int visit[maxn];
struct node
{
int arr[8];
int root;
};
void bfs()
{
for (int i = 0; i < maxn; i++)
visit[i] = 0;
int flag;
queue <node> q;
node u;
u.root = 0;
for (int i = 0; i < 8; i++)
u.arr[i] = i + 1;
q.push(u);
visit[0] = 1;
pre[0] = pii(-1, -1);
while (!q.empty())
{
node u = q.front(), vv;
q.pop();
// A
vv.arr[0] = u.arr[7];
vv.arr[1] = u.arr[6];
vv.arr[2] = u.arr[5];
vv.arr[3] = u.arr[4];
vv.arr[4] = u.arr[3];
vv.arr[5] = u.arr[2];
vv.arr[6] = u.arr[1];
vv.arr[7] = u.arr[0];
flag = cantor(vv.arr);
if (visit[flag] == 0)
{
visit[flag] = 1;
vv.root = flag;
pre[flag] = pii(u.root, 1);
q.push(vv);
}
// B
vv.arr[0] = u.arr[3];
vv.arr[1] = u.arr[0];
vv.arr[2] = u.arr[1];
vv.arr[3] = u.arr[2];
vv.arr[4] = u.arr[5];
vv.arr[5] = u.arr[6];
vv.arr[6] = u.arr[7];
vv.arr[7] = u.arr[4];
flag = cantor(vv.arr);
if (visit[flag] == 0)
{
visit[flag] = 1;
vv.root = flag;
pre[flag] = pii(u.root, 2);
q.push(vv);
}
// C
vv.arr[0] = u.arr[0];
vv.arr[1] = u.arr[6];
vv.arr[2] = u.arr[1];
vv.arr[3] = u.arr[3];
vv.arr[4] = u.arr[4];
vv.arr[5] = u.arr[2];
vv.arr[6] = u.arr[5];
vv.arr[7] = u.arr[7];
flag = cantor(vv.arr);
if (visit[flag] == 0)
{
visit[flag] = 1;
vv.root = flag;
pre[flag] = pii(u.root, 3);
q.push(vv);
}
}
}
int ed;
vector <int> ans;
void solve()
{
ans.clear();
while (pre[ed].fi != -1)
{
ans.pb(pre[ed].se);
ed = pre[ed].fi;
}
}
int main()
{
bfs();
string s1, s2;
while (cin >> s1 >> s2)
{
int arr[8];
map <char, int> mp;
for (int i = 0; i < 8; i++)
mp[s1[i]] = i + 1;
for (int i = 0; i < 8; i++)
arr[i] = mp[s2[i]];
ed = cantor(arr);
solve();
int len = ans.size();
for (int i = len - 1; i >= 0; i--)
printf("%c", 'A' + ans[i] - 1);
printf("\n");
}
}
HDU - 1430 魔板 【BFS + 康托展开 + 哈希】的更多相关文章
- hdu.1430.魔板(bfs + 康托展开)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- 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预处理 + 康托)
对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...
- hdu 1430 魔板 (BFS+预处理)
Problem - 1430 跟八数码相似的一题搜索题.做法可以是双向BFS或者预处理从"12345678"开始可以到达的所有状态,然后等价转换过去直接回溯路径即可. 代码如下: ...
- HDU_1430——魔板,预处理,康托展开,置换,string类的+操作
Problem Description 在魔方风靡全球之后不久,Rubik先生发明了它的简化版——魔板.魔板由8个同样大小的方块组成,每个方块颜色均不相同,可用数字1-8分别表示.任一时刻魔板的状态可 ...
- [HDU 1430] 魔板
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- hdu 1430 魔板 康托展开 + 很好的映射
http://acm.hdu.edu.cn/showproblem.php?pid=1430 如果从start ---> end,每一次都bfs进行,那么就肯定会超时. 考虑到先把start映射 ...
- HDU_1043 Eight 【逆向BFS + 康托展开 】【A* + 康托展开 】
一.题目 http://acm.hdu.edu.cn/showproblem.php?pid=1043 二.两种方法 该题很明显,是一个八数码的问题,就是9宫格,里面有一个空格,外加1~8的数字,任意 ...
随机推荐
- <p>在静态类junit.framework.Assert或者静态类org.junit.Assert中存在下面几个方法</p>
在静态类junit.framework.Assert或者静态类org.junit.Assert中存在下面几个方法 1.assertEquals()方法,用来查看对象中存的值是否是期待的值,与字符串比較 ...
- 【VBA】复制单元格批注
只复制单元格的批注该怎么操作呢?代码如下: Public Sub 复制单元格批注() Dim range1 As range Dim range2 As range '清除G列 Columns(&qu ...
- httpModules 与 httpHandlers
ASP.NET对请求处理的过程:当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给ASPNET_ISAPI.dll,AS ...
- 自己定义View时,用到Paint Canvas的一些温故,讲讲平时一些效果是怎么画的(基础篇 一)
转载请注明出处王亟亟的大牛之路 之前也有一个相似于画板的操作,可是不够具体,这边先补上链接.有兴趣的小伙伴能够看看http://blog.csdn.net/ddwhan0123/article/det ...
- 安装IntelliJ IDEA默认C盘文件过大怎么办
方法如下: 找到安装路径下有个属性文件,我的是在 D:\Program Files\JetBrains\IntelliJ IDEA 2017.3.2\bin 进入bin目录后找到属性文件:idea.p ...
- libpointmatcher的filter
Maximum Density Filter Points are only considered for rejection if they exceed a density threshold, ...
- MySQL四-2:完整性约束
阅读目录 一 介绍 二 not null与default 三 unique 四 primary key 五 auto_increment 六 foreign key 七 作业 一 介绍 约束条件与数据 ...
- Java 异常介绍
Java标准库内建了一些通用的异常,这些类以 Throwable 为顶层父类.Throwable又派生出 Error 类和 Exception 类. 错误:Error类以及他的子类的实例,代表了JVM ...
- Smart pointer 智能指针小总结
Smart pointer line 58之后smart pointer里的计数已经是0,所以会真正释放它引用的对象,调用被引用对象的析构函数.如果继续用指针访问,会出现如下图的内存访问异常.所以说如 ...
- HDU 3397 Sequence operation(区间合并 + 区间更新)
题目链接:pid=3397">http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意:给定n个数,由0,1构成.共同拥有5种操作. 每一个操 ...