hdu 1430 魔板(bfs+康拓展开)
2013-12-24 15:29
253 查看
魔板
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1585 Accepted Submission(s): 329
Problem Description
在魔方风靡全球之后不久,Rubik先生发明了它的简化版——魔板。魔板由8个同样大小的方块组成,每个方块颜色均不相同,可用数字1-8分别表示。任一时刻魔板的状态可用方块的颜色序列表示:从魔板的左上角开始,按顺时针方向依次写下各方块的颜色代号,所得到的数字序列即可表示此时魔板的状态。例如,序列(1,2,3,4,5,6,7,8)表示魔板状态为:
1 2 3 4
8 7 6 5
对于魔板,可施加三种不同的操作,具体操作方法如下:
A: 上下两行互换,如上图可变换为状态87654321
B: 每行同时循环右移一格,如上图可变换为41236785
C: 中间4个方块顺时针旋转一格,如上图可变换为17245368
给你魔板的初始状态与目标状态,请给出由初态到目态变换数最少的变换步骤,若有多种变换方案则取字典序最小的那种。
Input
每组测试数据包括两行,分别代表魔板的初态与目态。
Output
对每组测试数据输出满足题意的变换步骤。
Sample Input
12345678 17245368 12345678 82754631
Sample Output
C AC
Author
LL
Source
ACM暑期集训队练习赛(三)
题意:天朝文字不解释
题解:用bfs预处理又12345678到其他所有的步数,其中用康拓展开hash一下来记录是否访问,然后将输入的初始态对应到12345678,就可以直接得出答案~
#include<iostream> #include<cstring> #include<string> #include<queue> #include<algorithm> #define N 44520 using namespace std; int has[10]={1,1,2,6,24,120,720,5040,40320}; bool mark ; string ans ; struct point{ string s; string step; point(){} point(string s_,string step_) { s=s_; step=step_; } }; int myhas(string s) { int cou,res=0; for(int i=0;i<7;i++) { cou=0; for(int j=i+1;j<8;j++) { if(s[j]<s[i]) cou++; } res+=cou*has[7-i]; } return res; } void changea(string &s) { swap(s[0],s[7]); swap(s[1],s[6]); swap(s[2],s[5]); swap(s[3],s[4]); } void changeb(string &s) { char ch=s[3]; for(int i=3;i>0;i--) s[i]=s[i-1]; s[0]=ch; ch=s[4]; for(int i=4;i<7;i++) s[i]=s[i+1]; s[7]=ch; } void changec(string &s) { swap(s[1],s[6]); swap(s[6],s[5]); swap(s[5],s[2]); } void bfs() { queue<point>q; point now; string temp="12345678"; int k; memset(mark,false,sizeof(mark)); now=point(temp,""); mark[myhas(now.s)]=true; ans[myhas(now.s)][0]='\0'; q.push(now); while(!q.empty()) { now=q.front(); q.pop(); temp=now.s; changea(temp); k=myhas(temp); if(!mark[k]) { ans[k]=now.step+"A"; mark[k]=true; q.push(point(temp,now.step+"A")); } temp=now.s; changeb(temp); k=myhas(temp); if(!mark[k]) { ans[k]=now.step+"B"; mark[k]=true; q.push(point(temp,now.step+"B")); } temp=now.s; changec(temp); k=myhas(temp); if(!mark[k]) { ans[k]=now.step+"C"; mark[k]=true; q.push(point(temp,now.step+"C")); } } } int main() { string sta,fin; bfs(); while(cin>>sta>>fin) { string s; for(int i=0;i<8;i++) s[sta[i]-'0']=i+1; for(int i=0;i<8;i++) fin[i]=s[fin[i]-'0']+'0'; int k=myhas(fin); cout<<ans[k]<<endl; } return 0; }
相关文章推荐
- HDU 1430 魔板 康托展开或字典树 + BFS
- hdu 1430 魔板 康托展开 + 很好的映射
- hdu 1430 魔板
- hdu 1430 魔板 (BFS+预处理)
- HDU 1430 魔板
- HDU 1430 魔板 搜索
- hdu 1430 (BFS 康托展开 或 map )
- ACM-康托展开+预处理BFS之魔板——hdu1430
- [HDU 1430] 魔板
- HDU 1430 魔板(BFS+HASH+置换)
- HDU-1430 魔板
- HDU 1430 魔板
- HDU 1430 魔板
- HDU 1430 魔板 (BFS)
- HDU_1430——魔板,预处理,康托展开,置换,string类的+操作
- HDU 1043 Eight(BFS+康拓展开)
- HDU 1430 魔板 [BFS+康拓展开]【数学】
- hdu 1430 魔板
- [HDU 1430] 魔板
- hdu 1430 魔板(康托展开+BFS+巧妙转换)