Problem:
八数码问题也称为九宫问题。在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同。棋盘上还有一个空格,与空格相邻的棋子可以移到空格中。要求解决的问题是:给出一个初始状态和一个目标状态,找出一种从初始转变成目标状态的移动棋子步数最少的移动步骤。
输入:第一个3*3的矩阵是原始状态,第二个3*3的矩阵是目标状态。
输出:移动所用最少的步数
Input:
2 8 3
1 6 4
7 0 5
1 2 3
8 0 4
7 6 5
Output:
6
Solution 1:
#include<stdio.h>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
struct node {
int x, y;
int step;
int T[3][3];
}S,Node;
int A[3][3];
int B[3][3];
int X[4] = { 0,0,1,-1 };
int Y[4] = { 1,-1,0,0 };
bool test1(int x, int y) {