魔板 bfs() 预处理,记录每种状态。然后状态置换,(重点要用到全排列的hash记录状态)

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
***************************************************************************************************************************
状态置换&&bfs()&&全排列的hash
***************************************************************************************************************************
  1 #include<iostream>
  2 #include<string>
  3 #include<cstring>
  4 #include<cstdio>
  5 #include<cmath>
  6 #include<queue>
  7 using namespace std;
  8 #define maxn 50000
  9 bool vis[maxn];
 10 string anss[maxn];
 11 struct node
 12 {
 13     int state;
 14     char ch[10];
 15 }cur,now,que[maxn];
 16 int factorial[]={1,1,2,6,24,120,720,5040};
 17 char s[10],e[10];
 18 int get_ans(char*ss)//全排列的hash
 19 {
 20   int it,jt,cnt,val=0;
 21   for(it=0;it<8;it++)
 22   {
 23       cnt=0;
 24        for(jt=it+1;jt<8;jt++)
 25        if(ss[jt]<ss[it])
 26         cnt++;
 27       val+=cnt*factorial[7-it];
 28   }
 29   return val;
 30 }
 31 void getns(int k)//三种操作
 32 {
 33     int it,jt;
 34     if(k==0)
 35     {
 36         for(it=0;it<4;it++)
 37         {
 38             cur.ch[3-it]=now.ch[it+4];
 39             cur.ch[it+4]=now.ch[3-it];
 40         }
 41     }
 42     else
 43      if(k==1)
 44      {
 45        for(it=0;it<3;it++)
 46        {
 47            cur.ch[it+1]=now.ch[it];
 48            cur.ch[it+4]=now.ch[it+5];
 49        }
 50        cur.ch[0]=now.ch[3];
 51        cur.ch[7]=now.ch[4];
 52      }
 53      else
 54         if(k==2)
 55         {
 56           for(it=0;it<8;it++)
 57            cur.ch[it]=now.ch[it];
 58           cur.ch[1]=now.ch[6];
 59           cur.ch[2]=now.ch[1];
 60           cur.ch[5]=now.ch[2];
 61           cur.ch[6]=now.ch[5];
 62         }
 63 
 64 }
 65 void bfs()//bfs()记录每一种状态
 66 {
 67     int head=0;
 68     int tail=1;
 69     cur.state=0;
 70     char c;
 71     memset(vis,0,sizeof(vis));
 72     vis[0]=1;
 73     strcpy(cur.ch,"12345678");
 74     anss[0]="";
 75     que[0]=cur;
 76     while(head<tail)
 77     {
 78         int i,j;
 79         now=que[head];
 80         int sta=now.state;
 81         for(i=0;i<3;i++)
 82         {
 83             getns(i);
 84             int tst=get_ans(cur.ch);
 85             if(!vis[tst])
 86             {
 87                 vis[tst]=1;
 88                 c='A'+i;
 89                 anss[tst]=anss[sta]+c;
 90                 cur.state=tst;
 91                 que[++tail]=cur;
 92             }
 93 
 94         }
 95         head++;
 96     }
 97 }
 98 int main()
 99 {
100     bfs();//预处理
101     char cs[10];
102     while(scanf("%s %s",s,e)!=EOF)
103     {
104         //置换
105         for(int i=0;i<8;i++)
106          cs[s[i]-'0']=i+1+'0';
107         for(int i=0;i<8;i++)
108           e[i]=cs[e[i]-'0'];
109         int est=get_ans(e);
110         cout<<anss[est]<<endl;
111     }
112     return 0;
113 }
View Code

 

posted @ 2013-11-13 11:25  persistent codeants  阅读(713)  评论(0编辑  收藏  举报