您的位置:首页 > 编程语言 > C语言/C++

简单魔板[Special judge]

2015-08-29 19:53 405 查看
Description

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

其初始状态是

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的整数),表示最多容许的步数。

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

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

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

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

注意:如果不能达到,则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 ;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ sicily 深搜