hdu1430魔板(BFS+康托展开)
做这题先看:http://blog.csdn.net/u010372095/article/details/9904497
1 2 3 4
8 7 6 5
对于魔板,可施加三种不同的操作,具体操作方法如下:
A: 上下两行互换,如上图可变换为状态87654321
B: 每行同时循环右移一格,如上图可变换为41236785
C: 中间4个方块顺时针旋转一格,如上图可变换为17245368
给你魔板的初始状态与目标状态,请给出由初态到目态变换数最少的变换步骤,若有多种变换方案则取字典序最小的那种。
17245368
12345678
82754631
AC
for( i=0;i<8;i++)
pos[ch1[i]-'0']=i+1+'0';
for( i=0;i<8;i++)
des[i]=pos[str[i]-'0‘];
s=cantor(des);
上面一段代码的意思是:把起始目标看成了1,2,3,4,5,6,7,8 ;
列如:位置: 12345678 12345678
起初: 63728145 变 12345678
终点: 86372541 成 51234876
解释一下:初:6在第1个位,那么在终点中找6用1代替,3在第2个位,在终点中找3用2代替,依次类推。
一开始我们就先按 12345678 这样的顺序建立了一棵像树一样的,如果直接从初态不进行转变的话,那么我们的结果可能有很多的走法,有可能是先走A或B都可以到目标,有多条路时,但是先走了B的路径,必须要输出小的也就是从A开始的那条路,那怎么办呢,就可以用转化的思想了,把初始状态变成12345678,这样的话,我们一开始就是从这样的顺序算出来的!!所以必须先进行转换,在从目标往上找并记下路径,一直找到最终父节点:12345678.
#include<stdio.h>
#include<iostream>
#include<queue>
#include<stack>
#include<string.h>
using namespace std;
typedef struct nn
{
char ch[9];
int son; }node1;
typedef struct n
{
char way;//记录从父节点到当前节点的走法
int father;//记录当前节点的父节点的位置
}node2;
node2 Node[40321];//节点
int fac[8]={1,1,2,6,24,120,720,5040};//阶层
int cantor(char s[])//计算s在康托展开的位置,每一个都是独一无二的
{
int i,j,k,ans=0;
for(i=0;i<8;i++)
{
k=0;
for(j=i+1;j<8;j++)
if(s[i]>s[j])
k++;
ans+=k*fac[7-i];
}
return ans;
}
void BFS(char ch1[])
{
queue<node1>Q;
node1 now,next;
int i,son,e=0;
char tc;
son=cantor(ch1);
strcpy(now.ch,ch1);now.ch[8]='\0';
now.son=son;Node[son].father=0;//最终父节点为本本身
Q.push(now);
while(!Q.empty())
{
now=Q.front(); Q.pop();
//printf("%s ",now.ch);if(e==300)getchar();e++;用于调试
next=now;
for(i=0;i<4;i++) {tc=next.ch[i];next.ch[i]=next.ch[7-i];next.ch[7-i]=tc; }
son=cantor(next.ch); next.son=son;
if(Node[son].father==-1)
{
Node[next.son].father=now.son; Node[next.son].way='A';
Q.push(next);
} next=now;
tc=next.ch[3];for(i=3;i>0;i--) next.ch[i]=next.ch[i-1]; next.ch[0]=tc;
tc=next.ch[4];for(i=4;i<7;i++) next.ch[i]=next.ch[i+1]; next.ch[7]=tc;
son=cantor(next.ch); next.son=son;
if(Node[son].father==-1)
{
Node[next.son].father=now.son; Node[next.son].way='B';
Q.push(next);
} next=now;
tc=next.ch[5];next.ch[5]=next.ch[2];next.ch[2]=next.ch[1];next.ch[1]=next.ch[6];next.ch[6]=tc;
son=cantor(next.ch); next.son=son;
if(Node[son].father==-1)
{
Node[next.son].father=now.son; Node[next.son].way='C';
Q.push(next);
}
}
}
int main()
{
int c,s,i;
char str[9];
char ch1[9]={'1','2','3','4','5','6','7','8'};
for(i=0;i<40321;i++)
Node[i].father=-1; BFS(ch1);
char Way[10000];
while(cin>>ch1>>str)
{
char pos[10];
char des[8];
for( i=0;i<8;i++)//转换
pos[ch1[i]-'0']=i+1+'0';
for( i=0;i<8;i++)
des[i]=pos[str[i]-'0'];
s=cantor(des);
i=0;
while(0!=s)//从目标到初太搜索路径
{
Way[i++]=Node[s].way;//printf("7 ");
s=Node[s].father;
}
while(i--)//输出从初态到目标的路径
{
printf("%c",Way[i]);
}
printf("\n");
}
}
/*
63728145
86372541
ACBBBCBBCBCBCABB
下面是一步一步按顺序从队列中取300出来的:
12345678 87654321 41236785 17245368 58763214 82754631 34127856 48136275 86354271
41723685 16745238 65872143 51863724 13645728 58276314 83254761 23418567 3542718
6 57263184 34812756 47836125 58632714 24176853 48123765 41672385 76581432 645728
13 42736815 65187243 52163874 41367285 75823146 51876234 58327614 26318457 68172
453 23541867 38527416 65721843 13487562 35412876 34781256 35867142 51832674 7241
8536 25476183 56732184 24817653 46823175 74163852 48172635 73681542 31827546 764
58132 61472583 34278156 86512437 64587123 65218743 64132857 48167325 27581463 74
523816 43267815 75182346 53176824 25836147 51827364 75481362 12634578 25618347 7
6814532 42358671 26341587 23854167 26578431 64521783 81345627 16387452 67821453
13548762 37512486 83472561 35481726 63581427 34567812 47623815 35186742 57132864
73218456 38167452 72541836 28576413 35671842 12486537 25417863 24681753 6741852
3 75463182 53627184 74816352 43872165 24518637 87365421 74381652 23185467 576413
28 73658412 76145832 73421568 35478216 18654372 83612547 32178546 86451237 62487
513 16527438 64518273 36418572 65432187 52376184 64813257 42867135 26781543 6183
2547 27458163 71423586 64328157 87513462 74582136 75318246 32581476 24536817 463
72815 25183647 56127834 87543621 31265784 17234658 12563478 17685324 73614852 54
236718 47258361 78514362 42635871 28641357 52381674 26354817 72654318 23678541 3
8712546 26457831 68421573 82145367 25478361 81634527 15687342 26784531 41357628
16348572 13754862 78345612 86372451 62718453 83547261 62381547 21876543 63458127
31467582 24768153 83517426 34586172 35718642 65481237 17324568 63814527 7324158
6 72854136 73568421 34571682 81245376 13286457 36871452 12548637 26517483 824675
31 25481673 28136457 67541823 78563412 25361847 17483526 75416832 12456378 68734
215 82765341 87436521 82314675 26385147 45763281 52741638 21485637 57364128 7135
8642 47618325 73645182 27345681 76321458 61287453 73542168 31578426 17854632 745
21638 18365472 84312657 73215468 58642371 83651427 86245137 21654387 13627548 37
281546 16452738 37618452 78123456 36541872 68532417 75231846 16482573 65413827 6
4281357 34518762 82675431 36185472 26758413 27145863 26431578 65428317 18754623
86713542 63128547 87451362 73482516 17532468 74518326 71863542 32458176 21436587
74638152 82516473 24583167 48756213 82743561 63127845 38165274 85643271 3172658
4 15734268 61254783 17263548 81763245 12785634 25841637 17368524 75314682 514362
78 16385274 54723618 46758231 17853624 34268715 47235681 42863571 85236741 57281
364 71845362 52638174 71254638 14587632 72365418 24378651 13875462 52648317 2365
7481 26845731 76354128 48213675 72543618 82134657 81563427 82675314 23684751 541
36287 42157368 27584361 41635728 17648352 51378624 16354782 15427368
*/
hdu1430魔板(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预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- HDU_1430——魔板,预处理,康托展开,置换,string类的+操作
Problem Description 在魔方风靡全球之后不久,Rubik先生发明了它的简化版——魔板.魔板由8个同样大小的方块组成,每个方块颜色均不相同,可用数字1-8分别表示.任一时刻魔板的状态可 ...
- HDU_1043 Eight 【逆向BFS + 康托展开 】【A* + 康托展开 】
一.题目 http://acm.hdu.edu.cn/showproblem.php?pid=1043 二.两种方法 该题很明显,是一个八数码的问题,就是9宫格,里面有一个空格,外加1~8的数字,任意 ...
- HDU1430;魔板(BFS+康托展开)
传送门 题意 给出初始序列与终止序列,给出三种操作,问最少经过几次操作能使初始->终止,输出操作(字典序最小) 分析 字符串只有8个字符,使用康托展开. 1.BFS将所有序列从"123 ...
- 魔板 (bfs+康托展开)
# 10027. 「一本通 1.4 例 2」魔板 [题目描述] Rubik 先生在发明了风靡全球魔方之后,又发明了它的二维版本--魔板.这是一张有 888 个大小相同的格子的魔板: 1 2 3 4 8 ...
- HDU_1430 魔板 【BFS+康托展开+置换】
一.题面 POJ1430 二.分析 该题与之前做的八数码不同,它是一个2*4的棋盘,并且没有空的区域.这样考虑的情况是很少的,依然结合康托展开,这时康托展开最多也只乘7的阶乘,完全可以BFS先预处理一 ...
- HDU - 1430 魔板 【BFS + 康托展开 + 哈希】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...
随机推荐
- Storm技术结合
http://pan.baidu.com/s/1mhzj5XI?qq-pf-to=pcqq.group#path=%252F
- HDU 1176 免费馅饼(DP)
点我看题目 题意 : 中文题.在直线上接馅饼,能接的最多是多少. 思路 :这个题其实以前做过.....你将这个接馅饼看成一个矩阵,也不能说是一个矩阵,反正就是一个行列俱全的形状,然后秒当行,坐标当列, ...
- bitset学习小记
Cplusplus官网的资料: http://www.cplusplus.com/reference/bitset/bitset/ http://www.cplusplus.com/reference ...
- 滑动RecyclerView时出现异常: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state:30
RecyclerView 存在的一个明显的 bug 一直没有修复: java.lang.IndexOutOfBoundsException: Inconsistency detected. Inval ...
- WinForm实现简单的拖拽功能(C#)
用到了ListBox和TreeView两个控件,ListBox作为数据源,通过拖拽其中的数据放置到TreeView上,自动添加一个树节点 ListBox控件的MouseDown用于获取要拖拽的值并调用 ...
- WPF——文本随滚动条改变而改变
一.造一个窗体,拖进一个文本框TextBox和滚动条Slider 二.让文本框的内容随滚动条的滚动而改变,即文本框绑定到滚动条上 三.实现效果
- apache开源项目--gora
Gora 是一个应用于 NoSQL 数据库的 ORM 框架,支持包括:Apache HBase/Apache Cassandra
- jQuery on()方法绑定动态元素的点击事件
之前就一直受这个问题的困扰,在jQuery1.7版本之后添加了on方法,之前就了解过,其优越性高于live(),bind(),delegate()等方法,在此之前项目中想用这个来测试结果发现,居然动态 ...
- c#调用带有安全认证的java webservice
最近使用c#调用另外一个同事写的java webservice耽误了很多时间,网上资料不太完整,走了很多弯路,希望对大家有帮助. 基本思路是1.拼装soap使用http post ,主要将验证身份信息 ...
- Android使用HttpClient实现文件上传到PHP服务器,并监控进度条
上传 服务器端PHP 代码如下 : <?php $target_path = "./tmp/";//接收文件目录 $target_path = $target_path.($ ...