12345678 17245368 12345678 82754631
C AC
找到一种方案,从A->B。
预处理,将A字符串看成“12345678”,对应的将B字符串改变成对应的编号。
然后使用康托展开或者map标记都可以,储存对应结果。
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<queue> #include<map> using namespace std; struct node { string str; string s; } a,b; int len; map<string,int>Mp;// 标记出现过的状态,并编号 string ed[60000];//储存所有对应编号的状态变化顺序 void bfs() { queue<node>q; q.push(a); while(q.size()) { a=q.front(); q.pop(); b.str=a.str; b.s=a.s; //枚举所有移动方案,按字典序 b.s.append("A"); for(int i=0; i<4; i++) swap(b.str[i],b.str[7-i]); if(Mp[b.str]==0) { Mp[b.str]=len++; ed[len-1]=b.s; q.push(b); } b=a; b.s.append("B"); for(int i=3; i>=0; i--) { char ch; if(i-1>=0) ch=a.str[i-1]; else ch=a.str[3]; b.str[i]=ch; } for(int i=4; i<8; i++) { char ch; if(i+1<8) ch=a.str[i+1]; else ch=a.str[4]; b.str[i]=ch; } if(Mp[b.str]==0) { Mp[b.str]=len++; ed[len-1]=b.s; q.push(b); } b=a; b.s.append("C"); swap(b.str[1],a.str[6]); swap(b.str[2],a.str[1]); swap(b.str[5],a.str[2]); swap(b.str[6],a.str[5]); if(Mp[b.str]==0) { Mp[b.str]=len++; ed[len-1]=b.s; q.push(b); } } return ; } int main() { len=1; a.str=string("12345678"); a.s=string(""); Mp[a.str]=len++; ed[Mp[a.str]]=a.s; //预处理 bfs(); string c=string ("12345678"); while(cin>>a.str>>b.str) { for(int i=1; i<=8; i++) { char ch=i+'0'; for(int j=0; j<8; j++) { if(b.str[j]==a.str[i-1]) { c[j]=ch; break; } } } cout<<ed[Mp[c]]<<endl; } }