/*与魔板差不多,只是操作规则变了而已
*/
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
using namespace std;
int fact[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320}; //8!的阶乘表
bool isvisit[50000];
struct state{
int x;
int y;
string op;
};
/*
康托展开
*/
int cantor(long n)
{
int tmp[8];
int cnt;
for (int i = 7; i >= 0; i--)
{
tmp[i] = n%10;
n /= 10;
}
for (int i = 0; i < 7; i++)
{
cnt = 0;
for (int j = i+1; j < 8; j++)
if (tmp[i] >tmp[j]) cnt++;
n += fact[8-i-1] * cnt;
}
return n;
}
void OPA(int &x, int &y)
{
int x1 = x/1000;
int x2 = (x - x1*1000)/100;
int x3 = (x - x1*1000 - x2*100)/10;
int x4 = x - x1*1000 - x2*100 - x3*10;
int y1 = y/1000;
int y2 = (y - y1*1000)/100;
int y3 = (y - y1*1000 - y2*100)/10;
int y4 = y - y1*1000 - y2*100 - y3*10;
x = x3*1000 + x4*100 + x1*10+ x2;
y = y3*1000 + y4*100 + y1*10+ y2;
}
void OPB(int &x, int &y)
{
x = (x - x/1000 *1000) *10 + x/1000;
y = (y - y/1000 *1000) *10 + y/1000;
//cout << x << y << endl;
}
void OPC(int &x, int &y)
{
int x1 = x/1000;
int x2 = (x - x1*1000)/100;
int x3 = (x - x1*1000 - x2*100)/10;
int x4 = x - x1*1000 - x2*100 - x3*10;
int y1 = y/1000;
int y2 = (y - y1*1000)/100;
int y3 = (y - y1*1000 - y2*100)/10;
int y4 = y - y1*1000 - y2*100 - y3*10;
x = x1*1000 + x3*100 + y3*10 + x4;
y = y1*1000 + x2*100 + y2*10 + y4;
}
int main()
{
int n;
int x, y;
int x1,x2,x3,x4, y1,y2,y3,y4;
state qm[81000];
char opresult[300];
n = 0;
char op[3] = {'A', 'B', 'C'};
while(cin >> n && n != -1){
memset(isvisit, false, sizeof(isvisit));
cin >> x1 >> x2 >> x3 >> x4 >> y1 >> y2 >> y3 >> y4;
x = x1*1000+x2*100+x3*10+ x4;
y = y1*1000+y2*100+y3*10+ y4;
int tempx;
int tempy;
int fp=0;
int rp=1;
state init = {1234, 5678, ""};
qm[0] = init;
bool flag = true;
while(fp!=rp){
if(qm[fp].op.length()>n)
{
cout << -1 << endl;
break;
}
tempx = qm[fp].x;
tempy = qm[fp].y;
if(qm[fp].x == x && qm[fp].y == y){
cout << qm[fp].op.length() << " " << qm[fp].op << endl;
break;
}
for(int i=0; i<3 && flag; i++){
tempx = qm[fp].x;
tempy = qm[fp].y;
state temp;
if(op[i] == 'A')
{ OPA(tempx, tempy);
temp.op= qm[fp].op +"A";
}
if(op[i] == 'B')
{ OPB(tempx, tempy);
temp.op= qm[fp].op +"B";
}
if(op[i] == 'C')
{ OPC(tempx, tempy);
temp.op= qm[fp].op +"C";
}
temp.x = tempx;
temp.y = tempy;
if (!isvisit[cantor(tempx * 10000+ tempy)])
{
isvisit[cantor(tempx * 10000+ tempy)] = true;
qm[rp] = temp;
rp++;
}
}
fp++;
}
}
//system("pause");
return 0;
}
1515.魔板C
最新推荐文章于 2021-07-15 22:47:21 发布