简单魔板[Special judge]

Description

魔板由8个大小相同方块组成,分别用涂上不同颜色,用18的数字表示。

其初始状态是

1 2 3 4

8 7 6 5

对魔板可进行三种基本操作:

A操作(上下行互换):

8 7 6 5

1 2 3 4

B操作(每次以行循环右移一个):

4 1 2 3

5 8 7 6

C操作(中间四小块顺时针转一格):

1 7 2 4

8 6 3 5

用上述三种基本操作,可将任一种状态装换成另一种状态。
Input

输入包括多个要求解的魔板,每个魔板用三行描述。

第一行步数N不超过10的整数),表示最多容许的步数。

第二、第三行表示目标状态,按照魔板的形状,颜色用18的表示。

当N等于-1的时候,表示输入结束。
Output

对于每一个要求解的魔板,输出一行。

首先是一个整数M,表示你找到解答所需要的步数。接着若干个空格之后,从第一步开始按顺序给出M步操作(每一步是ABC),相邻两个操作之间没有任何空格。

注意:如果不能达到,则M输出-1即可。



// Problem#: 1150
// Submission#: 3823447
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
// easy_moban.cpp
#include "iostream"
#include "string.h"
using namespace std ;

class moban{
    public:
    int x[4] ;
    int y[4] ;
    moban(){
        memset( x , 0 , sizeof(x)) ;
        memset( y , 0 , sizeof(y)) ;
    }
    moban (int x1 , int x2 , int x3 , int x4 , int y1 , int y2 , int y3 , int y4){
        x[0] = x1 ; x[1] = x2 ; x[2] = x3 ; x[3] = x4 ;
        y[0] = y1 ; y[1] = y2 ; y[2] = y3 ; y[3] = y4 ;
    }
    bool operator==(moban &m2){
        for(int i = 0  ; i < 4 ; i ++ ){
            if((m2.x[i] != this -> x[i] ) && (m2.y[i] != this -> y[1] ) ){
                return false ;
            }
        }

        return true ; 
    }
    bool operator!=(moban &m2){
        return !(*(this) == m2 ) ;
    }
};

moban stan_moban = moban( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ) ;
int stan_dep = 0 , flag = 0 ;


void dfs(moban m , int dep , string now ){
    if (dep > stan_dep){
        return ;
    }
    else if ( (dep == stan_dep) && (m != stan_moban) ){
        return ;
    }
    if( m == stan_moban ){
        cout << dep << endl << now << endl ;
        flag = 1 ;
        return ;
    }
    if(flag == 0){
        dfs(moban(m.y[0] , m.y[1] , m.y[2] , m.y[3] , m.x[0] , m.x[1] , m.x[2] , m.x[3]) , dep + 1 , now + 'A' ) ;  
    }
    if(flag == 0 ){
        dfs(moban(m.x[1] , m.x[2] , m.x[3] , m.x[0] , m.y[1] , m.y[2] , m.y[3] , m.y[0]) , dep + 1 , now + 'B' ) ;  
    }
    if(flag == 0 ){
        dfs(moban(m.x[0] , m.x[2] , m.y[2] , m.x[3] , m.y[0] , m.x[1] , m.y[1] , m.y[3]) , dep + 1 , now + 'C' ) ;  
    }
    if(flag == 0 && dep == 0 ){
        cout << -1 << endl ;    
    }
    return ;
}

int main (){
    while (cin >> stan_dep && stan_dep != -1){
        int x1 , x2 , x3 , x4 , x5 ,x6 , x7 , x8 ;
        cin >> x1 >> x2 >> x3 >> x4 >> x5 >> x6 >> x7 >> x8 ;
        moban aim = moban(x1 , x2 , x3 , x4 , x5 , x6 , x7 , x8 ) ;
        dfs(aim , 0 , "") ;
    }
    return 0 ;

}                                 


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值