#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
#define N 10
int n;
int mat[N][N];
int pos=0;
int ans[100];
bool read()
{
if(cin>>mat[1][3]&&mat[1][3]) {
cin>>mat[1][5];
cin>>mat[2][3];
cin>>mat[2][5];
for(int i=1;i<=7;i++) cin>>mat[3][i];
cin>>mat[4][3]>>mat[4][5];
for(int i=1;i<=7;i++) cin>>mat[5][i];
cin>>mat[6][3]>>mat[6][5];
cin>>mat[7][3]>>mat[7][5];
return 1;
}
return 0;
}
void aopr()
{
int buff=mat[1][3];
for(int i=1;i<=6;i++) mat[i][3]=mat[i+1][3];
mat[7][3]=buff;
}
void bopr()
{
int buff=mat[1][5];
for(int i=1;i<=6;i++) mat[i][5]=mat[i+1][5];
mat[7][5]=buff;
}
void copr()
{
int buff=mat[3][7];
for(int i=7;i>=2;i--) mat[3][i]=mat[3][i-1];
mat[3][1]=buff;
}
void dopr()
{
int buff=mat[5][7];
for(int i=7;i>=2;i--) mat[5][i]=mat[5][i-1];
mat[5][1]=buff;
}
void eopr()
{
int buff=mat[7][5];
for(int i=7;i>=2;i--) mat[i][5]=mat[i-1][5];
mat[1][5]=buff;
}
void fopr()
{
int buff=mat[7][3];
for(int i=7;i>=2;i--) mat[i][3]=mat[i-1][3];
mat[1][3]=buff;
}
void gopr()
{
int buff=mat[5][1];
for(int i=1;i<=6;i++) mat[5][i]=mat[5][i+1];
mat[5][7]=buff;
}
void hopr()
{
int buff=mat[3][1];
for(int i=1;i<=6;i++) mat[3][i]=mat[3][i+1];
mat[3][7]=buff;
}
void opr(int op)
{
if(op==1) aopr();
else if(op==2) bopr();
else if(op==3) copr();
else if(op==4) dopr();
else if(op==5) eopr();
else if(op==6) fopr();
else if(op==7) gopr();
else hopr();
}
void rop(int op)
{
if(op==1) fopr();
else if(op==2) eopr();
else if(op==3) hopr();
else if(op==4) gopr();
else if(op==5) bopr();
else if(op==6) aopr();
else if(op==7) dopr();
else if(op==8) copr();
}
int find()
{
int num[5];for(int i=1;i<=3;i++) num[i]=0;
for(int i=3;i<=5;i++) ++num[mat[3][i]];
num[mat[4][3]]++;
num[mat[4][5]]++;
for(int i=3;i<=5;i++) ++num[mat[5][i]];
return max(max(num[1],num[2]),num[3]);
}
void dfs(int now,int depth)
{
if(pos) return;
int buff=find();
//cout<<now<<' ' <<buff<<' '<<depth<<endl;
if(now+8-buff>depth) return;
if(now>depth) return;//顶多差一层
if(buff==8)
{
if(now==0) {printf("No moves needed\n");printf("%d\n",mat[3][3]);pos=1;}
else
{
for(int i=0;i<depth;i++) printf("%c",ans[i]+'A'-1);
printf("\n%d\n",mat[3][3]);
pos=1;
}
return;
}
for(int i=1;i<=8;i++) {if(pos) return ;opr(i);ans[now]=i;dfs(now+1,depth);rop(i);}
}
void init()
{
pos=0;
memset(mat,0,sizeof(mat));
// memset(num,0,sizeof(num));
}
void solve()
{
int depth;
for(depth=0;;depth++)
{
dfs(0,depth);
if(pos) break;
}
}
int main()
{
while(read())
{
solve();
init();
}
/*read();
int op;
while(cin>>op)
{
opr(op);
for(int i=1;i<=7;i++)
{
for(int j=1;j<=7;j++) cout<<mat[i][j]<<' ';
cout<<endl;
}
}*/
return 0;
}
很简单的ida* 这个剪枝只要一个就好了! 就是 now+ 8 - find()> depth 就好了
The Rotation Game uva1343
最新推荐文章于 2021-07-02 22:00:04 发布