注意基本状态的存储方式,模拟好三种变换,对每个状态做三种变换广搜即可。
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i = a;i<n;i++)
#define per(i,a,n) for(int i = n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define yes cout<<"YES"<<'\n';
#define no cout<<"NO"<<'\n';
#define endl '\n';
#define R register
typedef vector<int> VI;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll MOD=1000000007;
int rnd(int x) {return mrand() % x;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;};
ll lcm(int a,int b){return a*b/gcd(a,b);};
string s="12348765";
string ans="ZZZZ";
unordered_set<string> st;
string ss;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
rep(i,0,8){
int x;
cin>>x;
ss+='0'+x;
}
reverse(ss.begin()+4,ss.end());
if(ss==s){
cout<<0<<endl;
return 0;
}
queue<pair<string,string>> q;
st.insert(s);
q.push({s,""});
while(!q.empty()){
int sz=SZ(q);
while(sz--){
auto [a,b]=q.front();
q.pop();
string m=a;
string k=b+'A';
rep(i,0,4) swap(m[i],m[i+4]);
if(!st.count(m)){
st.insert(m);
q.push({m,k});
if(m==ss) ans=min(ans,k);
}
m=a;
k=b+'B';
reverse(m.begin(),m.begin()+3);
reverse(m.begin(),m.begin()+4);
reverse(m.begin()+4,m.begin()+7);
reverse(m.begin()+4,m.end());
if(!st.count(m)){
st.insert(m);
q.push({m,k});
if(m==ss) ans=min(ans,k);
}
m=a;
k=b+'C';
swap(m[1],m[2]);
swap(m[1],m[5]);
swap(m[5],m[6]);
if(!st.count(m)){
st.insert(m);
q.push({m,k});
if(m==ss) ans=min(ans,k);
}
}
if(ans!="ZZZZ") break;
}
cout<<SZ(ans)<<endl;
cout<<ans<<endl;
return 0;
}