1515.魔板C

/*与魔板差不多,只是操作规则变了而已   
*/
#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;
}                                 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值