hdu 5012 bfs 康托展开
Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 491 Accepted Submission(s): 290
At the beginning, the two dices may face different(which means there exist some i, ai ≠ bi). Ddy wants to make the two dices look the same from all directions(which means for all i, ai = bi) only by the following four rotation operations.(Please read the picture for more information) Now Ddy wants to calculate the minimal steps that he has to take to achieve his goal.
For each case, the first line consists of six integers a1,a2,a3,a4,a5,a6, representing the numbers on dice A.
The second line consists of six integers b1,b2,b3,b4,b5,b6, representing the numbers on dice B.
1 2 3 4 5 6
1 2 3 4 5 6
1 2 5 6 4 3
1 2 3 4 5 6
1 4 2 5 3 6
3
-1
卜神的代码
为了测试康托展开, 文中注释部分是卜神原来的代码,也是ac的。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; typedef long long ll; const int CANTO = ;
const int LEN = ; int fac[];
bool vis[CANTO];
int begin, end;
struct Sit
{
int arr[LEN];
int step;
}; void makefac()
{
fac[] = fac[] = ;
for(int i = ; i <= ; i++)
fac[i] = i * fac[i-];
} int canto(int arr[])
{
int res = ;
for(int i = ; i < LEN; i++){
int num=;
for(int j=i+;j<LEN;j++)
if(arr[j]<arr[i]) num++;
res+=(num*fac[LEN-i-]);
}
// res += fac[i+2] * arr[i];
return res;
} int bfs(Sit src)
{
queue <Sit> q;
Sit now, tmp;
int t, c;
vis[begin] = true;
q.push(src);
while(!q.empty())
{
now = q.front();
q.pop();
tmp = now, tmp.step++; t = tmp.arr[];
tmp.arr[] = tmp.arr[];
tmp.arr[] = tmp.arr[];
tmp.arr[] = tmp.arr[];
tmp.arr[] = t;
c = canto(tmp.arr);
if (c == end){
return tmp.step;
}
if (!vis[c]){
vis[c] = true;
q.push(tmp);
}
tmp = now, tmp.step++; t = tmp.arr[];
tmp.arr[] = tmp.arr[];
tmp.arr[] = tmp.arr[];
tmp.arr[] = tmp.arr[];
tmp.arr[] = t;
c = canto(tmp.arr);
if (c == end){
return tmp.step;
}
if (!vis[c]){
vis[c] = true;
q.push(tmp);
} tmp = now, tmp.step++; t = tmp.arr[];
tmp.arr[] = tmp.arr[];
tmp.arr[] = tmp.arr[];
tmp.arr[] = tmp.arr[];
tmp.arr[] = t;
c = canto(tmp.arr);
if (c == end){
return tmp.step;
}
if (!vis[c]){
vis[c] = true;
q.push(tmp);
} tmp = now, tmp.step++; t = tmp.arr[];
tmp.arr[] = tmp.arr[];
tmp.arr[] = tmp.arr[];
tmp.arr[] = tmp.arr[];
tmp.arr[] = t;
c = canto(tmp.arr);
if (c == end){
return tmp.step;
}
if (!vis[c]){
vis[c] = true;
q.push(tmp);
}
}
return -;
} int main()
{
makefac();
int src[LEN];
int dst[LEN];
while(~scanf("%d", src)){
memset(vis, , sizeof(vis));
for(int i = ; i < LEN; i++)
scanf("%d", src+i);
for(int i = ; i < LEN; i++)
scanf("%d", dst+i);
begin = canto(src);
end = canto(dst);
if (begin == end){
printf("0\n");
continue;
}
Sit x;
memcpy(x.arr, src, LEN*sizeof(int));
x.step = ;
printf("%d\n", bfs(x));
}
return ;
}
经过以下代码验证,网上的代码,康托展开是连续值,卜神的只是起到了展开作用,不过代码更加简洁。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; typedef long long ll; const int CANTO = ;
const int LEN = ; int fac[];
int a[LEN]; void makefac()
{
fac[] = fac[] = ;
for(int i = ; i <= ; i++)
fac[i] = i * fac[i-];
} int canto(int arr[])
{
int res = ;
for(int i = ; i < LEN; i++){
int num=;
for(int j=i+;j<LEN;j++)
if(arr[j]<arr[i]) num++;
res+=(num*fac[LEN-i-]);
}
// res += fac[i+2] * arr[i];
return res;
} int canto2(int arr[])
{
int res = ;
for(int i = ; i < LEN; i++){
// int num=0;
// for(int j=i+1;j<LEN;j++)
// if(arr[j]<arr[i]) num++;
// res+=(num*fac[LEN-i-1]);
res += fac[i+] * arr[i];
}
// res += fac[i+2] * arr[i];
return res;
} int main()
{
freopen("data.out","w",stdout);
makefac();
a[]=;a[]=;a[]=;a[]=;a[]=;a[]=;
printf("a=%d %d %d %d %d %d ",a[],a[],a[],a[],a[],a[]);
printf("can=%d %d\n",canto(a),canto2(a));
while(next_permutation(a+, a + LEN) ){
printf("a=%d %d %d %d %d %d ",a[],a[],a[],a[],a[],a[]);
printf("can=%d %d\n",canto(a),canto2(a));
}
return ;
}
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
a= can=
hdu 5012 bfs 康托展开的更多相关文章
- hdu 1430 (BFS 康托展开 或 map )
第一眼看到这题就直接BFS爆搜,第一发爆了内存,傻逼了忘标记了,然后就改,咋标记呢. 然后想到用map函数,就8!个不同的排列,换成字符串用map标记.然后又交一发果断超时,伤心,最恨超时,还不如来个 ...
- hdu 1430(BFS+康托展开+映射+输出路径)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- HDU_1043 Eight 【逆向BFS + 康托展开 】【A* + 康托展开 】
一.题目 http://acm.hdu.edu.cn/showproblem.php?pid=1043 二.两种方法 该题很明显,是一个八数码的问题,就是9宫格,里面有一个空格,外加1~8的数字,任意 ...
- HDU 1043 Eight(双向BFS+康托展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
- HDU - 1430 魔板 【BFS + 康托展开 + 哈希】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...
- POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3
http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...
- 【HDU - 1043】Eight(反向bfs+康托展开)
Eight Descriptions: 简单介绍一下八数码问题:在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图: 1 2 3 4 5 6 7 8 在上图中,由于右下角位置是空的 ...
- hdu.1430.魔板(bfs + 康托展开)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- loj 1165(bfs+康托展开)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...
随机推荐
- jQuery备忘录
jquery 中遍历数组 var arr = [1,2,3,4,5] $.each(arr,function(i,j){ console.log(i,j) }) 结果 0 1 1 2 .... jQu ...
- vba练习资料
链接:https://pan.baidu.com/s/1E0e58rZ_3QCCorWNM-ehSA 提取码:jluf
- Java8特性详解 lambda表达式 Stream【转】
本文转自http://www.cnblogs.com/aoeiuv/p/5911692.html 1.lambda表达式 Java8最值得学习的特性就是Lambda表达式和Stream API,如果有 ...
- odoo Windows10启动debug模式报错(Process finished with exit code -1073740940 (0xC0000374))
之前用win10系统,安装odoo总是启动debug模式启动不起来很恼火. 报错问题:Process finished with exit code -1073740940 (0xC0000374) ...
- 数据结构( Pyhon 语言描述 ) — — 第7章:栈
栈概览 栈是线性集合,遵从后进先出原则( Last - in first - out , LIFO )原则 栈常用的操作包括压入( push ) 和弹出( pop ) 栈的应用 将中缀表达式转换为后缀 ...
- DocView mode 2 -- 快捷键
** 启动 C-c C-c 切换DocView和文件内容显示 M-x doc-view-mode 启动主模式 M-x doc-view-minor-mode 启动辅模式 k k ...
- mysql复制延迟排查
今天收到报警,提示从库延时,首先当然是上去查看情况,首先查看机器负载,如下: 可以看到使用cpu已经100%,io没有等待.那么查看mysql是什么情况,执行show processlist没有发现任 ...
- 算法学习记录-排序——冒泡排序(Bubble Sort)
冒泡排序应该是最常用的排序方法,我接触的第一个排序算法就是冒泡,老师也经常那这个做例子. 冒泡排序是一种交换排序, 基本思想: 通过两两比较相邻的记录,若反序则交换,知道没有反序的记录为止. 例子: ...
- Knockout v3.4.0 中文版教程-16-控制流-foreach绑定
2. 控制流 1. foreach绑定 目的 foreach绑定会遍历一个数组,为每个数组项生成重复的元素标记结构并做关联.这在渲染列表或表格的时候特别有用. 假设你的数组是一个监控数组,之后无论你进 ...
- python算法-二叉树广度优先遍历
广度优先遍历:优先遍历兄弟节点,再遍历子节点 算法:通过队列实现-->先进先出 广度优先遍历的结果: 50,20,60,15,30,70,12 程序遍历这个二叉树: # encoding=utf ...