关于c或c++的一道编程题(有点像旋转矩阵)

来源:百度知道 编辑:UC知道 时间:2024/06/05 01:58:28

2 8 3
1 6 4
7 5
通过多次局部移动得到如下结果

1 2 3
8 4
7 6 5

要求:
1.用C或C++编程
2.每次移动,只有空白处上下左右处的数字能移动,且只能移动到空白处
3.要求最终除了输出上面的结果图外,还要输出得出最终结果所经历的每一种状态.如:
2 8 3 2 8 3 2 8 3 2 3 2 3 1 2 3 1 2 3
1 6 4 1 6 4 1 4 1 8 4 1 8 4 8 4 8 4
7 5 7 5 7 6 5 7 6 5 7 6 5 7 6 5 7 6 5

拜求!最好加上注释!兄弟以人格担保,好的话追加分数!多谢多谢!!!
不好意思,输出状态写乱了应该像是这样:
2 8 3
1 6 4
7 5

2 8 3
1 6 4
7 5

2 8 3
1 4
7 6 5
.............等等,直到正确结果!

/*----------------------------------------------------
C++的八数码的原代码
-----------------------------------------------------*/
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
const int ROW = 3;
const int COL = 3;
const int MAXDISTANCE = 10000;
const int MAXNUM = 10000;
typedef struct _Node{
int digit[ROW][COL];
int dist; // distance between one state and the destination
int dep; // the depth of node
// So the comment function = dist + dep.
int index; // point to the location of parent
} Node;
Node src, dest;
vector<Node> node_v; // store the nodes
bool isEmptyOfOPEN() {
for (int i = 0; i < node_v.size(); i++) {
if (node_v[i].dist != MAXNUM)
return false;
}
return true;
}
bool isEqual(int index, int digit[][COL]) {
for (int i = 0; i < ROW; i++)
for (int j = 0; j < C