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 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...
随机推荐
- 讲课笔记3——CSS
背景常见样式: width:600px; height:800px; background: url(bg.jpg) no-repeat 40px 20px fixed gray ; * ...
- python在d盘,robotframework引入seleniumlibrary报错
在*** setting*** 中引入库 Library SeleniumLibrary 报错 unknown seleniumlibrary library ,try to use quic ...
- 火狐浏览器返回不加载JS
火狐浏览器 go(-1),返回后不加载JS,谷歌会加载. 总结: Firefox和Safari在back时不会触发load, ready事件! 解决方法: $(window).unload(funct ...
- js 返回上一页并刷新页面
js 方法 代码如下 self.location=document.referrer;
- ios之UIAlertView
举例: UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"messa ...
- (43)zabbix报警媒介介绍
zabbix触发器到了要发送通知的情况下,需要一个中间介质来接收并传递它的消息给运维们,以往用nagios,通常用脚本发送邮件或者发送飞信来达到报警.这个脚本实际上就是一个媒介了. zabbix有如下 ...
- perl学习之:理解贪婪匹配和最小匹配之间的区别
正则表达式的新手经常将贪婪匹配和最小匹配理解错误.默认情况下,Perl 的正则表达式是“贪婪地”,也就是说它们将尽可能多地匹配字符. 下面的脚本打印出“matched defgabcdef”,因为它尽 ...
- Python的第3堂课
20181119笔记 一.内存管理相关 ①Cpython解释器的垃圾回收机制 什么是垃圾:当一个值没有被绑定任何变量名(即该值的引用计数为零时),该值就是垃圾. 垃圾回收是收回值占用的内存空间. 引用 ...
- 【笔记】PIL 中的 Image 模块
Image 模块提供了一个同名类(Image),也提供了一些工厂函数,包括从文件中载入图片和创建新图片.例如,以下的脚本先载入一幅图片,将它旋转 45 度角,并显示出来: 1 >>> ...
- Python中的socket网络编程(TCP/IP,UDP)讲解
在网络编程中的一个基本组件就是套接字(socket).套接字基本上是两个端点的程序之间的"信息通道".程序可能分布在不同的计算机上,通过套接字互相发送信息.套接字包括两个:服务器套 ...