分析:IDA*往下搜就行了,刚开始想不写估价水过去,想多了,还得写估价。我们可以发现,每旋转一次,不同的数字最多会减少1,即相同的数字最多会增加一。所以我们找到1,2,3中目前最多的那个数的数目,然后用8减去这个数,就是我们至少需要旋转的次数。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <set>
#define FRER() freopen("in.txt","r",stdin)
#define FREW() freopen("out.txt","w",stdout)
#define go int T;cin>>T;for(int kase=0;kase<T;kase++)
#define debug cout<<"****************"<<endl
#define lowbit(x) x&(-x)
#define eps 1e-6
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int maxn = 50000 + 7;
int a[30];
char s[maxn];
const int op[8][7] = {
1,3,7,12,16,21,23,
2,4,9,13,18,22,24,
11,10,9,8,7,6,5,
20,19,18,17,16,15,14,
24,22,18,13,9,4,2,
23,21,16,12,7,3,1,
14,15,16,17,18,19,20,
5,6,7,8,9,10,11
};
const int t[] = {7,8,9,12,13,16,17,18};
bool read(){
int n;
cin>>n;
if(!n) return false;
a[1] = n;
for(int i=2;i<=24;i++) scanf("%d",&a[i]);
return true;
}
bool ok(){
if(a[7]==a[8]&&a[7]==a[9]&&a[7]==a[12]&&a[7]==a[13]&&a[7]==a[16]&&a[7]==a[17]&&a[7]==a[18]) return true;
return false;
}
void rotate(int i){
int tmp = a[op[i][0]];
for(int j=0;j<6;j++) a[op[i][j]] = a[op[i][j+1]];
a[op[i][6]] = tmp;
}
void rrotate(int i){
int tmp = a[op[i][6]];
for(int j=6;j>0;j--) a[op[i][j]] = a[op[i][j-1]];
a[op[i][0]] = tmp;
}
int cal(){
int ans1 = 0 , ans2 = 0 , ans3 = 0;
for(int i=0;i<8;i++){
if(a[t[i]] == 1) ans1 ++;
if(a[t[i]] == 2) ans2 ++;
if(a[t[i]] == 3) ans3 ++;
}
int ans = max(ans1,max(ans2,ans3));
return 8 - ans;
}
bool dfs(int now,int depth){
if(now > depth || now + cal() > depth) return false;
if(now==depth&&ok()) return true;
for(int i=0;i<8;i++){
s[now] = i + 'A';
rotate(i);
if(dfs(now+1, depth)) return true;
rrotate(i);
}
return false;
}
int main(){
//FRER();
//FREW();
while(read()){
if(ok()) {cout << "No moves needed" << endl<<a[7]<<endl;continue;}
for(int i=1;;i++){
if(dfs(0,i)){
for(int j=0;j<i;j++){
cout<<s[j];
}
cout<<endl;
cout<<a[7]<<endl;
break;
}
}
}
return 0;
}