#include <bits/stdc++.h> using namespace std; const int maxn = 10; int fact[8] = {1, 1, 2, 6, 24, 120, 720, 5040}; char op[maxn]; char ed[maxn]; map<int, int> mp; int terminal[maxn]; int change[3][8] = {{7, 6, 5, 4, 3, 2, 1, 0}, {3, 0, 1, 2, 5, 6, 7, 4}, {0, 6, 1, 3, 4, 2, 5, 7}}; string ans[40320+10]; bool vis[40320+10]; struct Node{ int num[maxn]; }; int get_hash(int* num){ int h_value = 0; int cnt = 0; for(int i=0; i<8; i++){ cnt = 0; for(int j=i+1; j<8; j++){ if(num[j]<num[i]) cnt++; } h_value += cnt*fact[7-i]; } return h_value; } void bfs(){ Node s; for(int i=1; i<=8; i++){ s.num[i-1] = i; } int init_value = get_hash(s.num); vis[init_value] = true; queue<Node> q; q.push(s); while(!q.empty()){ Node temp = q.front(); q.pop(); for(int i=0; i<3; i++){ Node cur; for(int j=0; j<8; j++){ cur.num[j] = temp.num[change[i][j]]; } int pre_value = get_hash(temp.num); int h_value = get_hash(cur.num); if(!vis[h_value]){ q.push(cur); vis[h_value] = true; ans[h_value] = ans[pre_value]+char('A'+i); } } } } int main() { bfs(); while(~scanf("%s%s", op, ed)){ mp.clear(); for(int i=0; i<8; i++){ mp.insert(make_pair(op[i]-'0', i+1)); } for(int i=0; i<8; i++){ terminal[i] = mp[ed[i]-'0']; } int h_value = get_hash(terminal); cout<<ans[h_value]<<endl; } return 0; }
|