题目链接:点我
#include <stdio.h>
#include <queue>
#include <map>
#include <vector>
using namespace std;
struct Node {
vector<char> steps;
//魔板
int matrix[2][4];
} startNode, node;
map<int, bool> intMapBool;
int targetMatrix[2][4], tempMatrix[2][4];
//把matrix2对应的值赋值给matrix1
void fillMatrix(int matrix1[][4], int matrix2[][4]) {
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 4; j++) {
matrix1[i][j] = matrix2[i][j];
}
}
}
//转换为int数值通过魔板
int toIntByMatrxi(int matrix[][4]) {
int result = 0;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 4; j++) {
result = result * 10 + matrix[i][j];
}
}
return result;
}
bool isEqual(int matrix[][4]) {
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 4; j++) {
if(matrix[i][j] != targetMatrix[i][j]) {
return false;
}
}
}
return true;
}
void bfs() {
//清除上次保留的步骤
startNode.steps.clear();
//清除映射
intMapBool.clear();
intMapBool[toIntByMatrxi(startNode.matrix)] = true;
queue<Node> q;
q.push(startNode);
Node tempNode;
while(!q.empty()) {
Node top = q.front();
//判断是否到达目标魔板
if(isEqual(top.matrix)) {
printf("%d\n", top.steps.size());
for(vector<char>::iterator it = top.steps.begin(); it != top.steps.end(); it++) {
printf("%c", *it);
}
printf("\n");
return;
}
q.pop();
for(int i = 0; i < 3; i++) {
switch(i) {
case 0:
//操作A:交换上下两行;
for(int j = 0; j < 4; j++) {
tempNode.matrix[0][j] = top.matrix[1][j];
}
for(int j = 0; j < 4; j++) {
tempNode.matrix[1][j] = top.matrix[0][j];
}
top.steps.push_back('A');
break;
case 1:
//操作B:将最右边的一列插入最左边;
tempNode.matrix[0][0] = top.matrix[0][3];
tempNode.matrix[1][0] = top.matrix[1][3];
for(int j = 0; j < 2; j++) {
for(int k = 1; k < 4; k++) {
tempNode.matrix[j][k] = top.matrix[j][k-1];
}
}
top.steps.push_back('B');
break;
case 2:
//操作C:魔板中央四格作顺时针旋转。
tempNode.matrix[0][1] = top.matrix[1][1];
tempNode.matrix[0][2] = top.matrix[0][1];
tempNode.matrix[1][1] = top.matrix[1][2];
tempNode.matrix[1][2] = top.matrix[0][2];
tempNode.matrix[0][0] = top.matrix[0][0];
tempNode.matrix[1][0] = top.matrix[1][0];
tempNode.matrix[0][3] = top.matrix[0][3];
tempNode.matrix[1][3] = top.matrix[1][3];
top.steps.push_back('C');
break;
}
//满足条件,入队
int value = toIntByMatrxi(tempNode.matrix);
if(intMapBool[value] == false) {
//把tempNode.matrix对应的值赋值给node.matrix
fillMatrix(node.matrix, tempNode.matrix);
//将top的步骤赋值给node
node.steps = top.steps;
q.push(node);
intMapBool[value] = true;
}
//移除最后一个操作
top.steps.pop_back();
}
}
}
int main() {
int arr[] = {1, 2, 3, 4, 8, 7, 6, 5};
//初始化startNode.matrix
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 4; j++) {
startNode.matrix[i][j] = arr[i*4+j];
}
}
while(~scanf("%d", &targetMatrix[0][0])) {
for(int i = 1; i < 4; i++) {
scanf("%d", &targetMatrix[0][i]);
}
for(int i = 3; i >= 0; i--) {
scanf("%d", &targetMatrix[1][i]);
}
bfs();
}
return 0;
}
在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板。这是一张有8个大小相同的格子的魔板:
1 2 3 4
8 7 6 5
我们知道魔板的每一个方格都有一种颜色。这8种颜色用前8个正整数来表示。可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列。对于上图的魔板状态,我们用序列(1,2,3,4,5,6,7,8)来表示。这是基本状态。
这里提供三种基本操作,分别用大写字母“A”,“B”,“C”来表示(可以通过这些操作改变魔板的状态):
“A”:交换上下两行;
“B”:将最右边的一列插入最左边;
“C”:魔板中央四格作顺时针旋转。
下面是对基本状态进行操作的示范:
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
对于每种可能的状态,这三种基本操作都可以使用。
你要编程计算用最少的基本操作完成基本状态到目标状态的转换,输出基本操作序列。
【输入格式】
输入有多组测试数据
只有一行,包括8个整数,用空格分开(这些整数在范围 1——8 之间),表示目标状态。
【输出格式】
Line 1: 包括一个整数,表示最短操作序列的长度。
Line 2: 在字典序中最早出现的操作序列,用字符串表示,除最后一行外,每行输出60个字符。
Sample Input
2 6 8 4 5 7 3 1
Sample Output
7
BCABCCB