|
|
@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define N 3 // 定义拼图的维度,这是一个3x3的拼图
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct Node {
|
|
|
|
|
|
|
|
int puzzle[N][N]; // 存储拼图状态的数组
|
|
|
|
|
|
|
|
struct Node* parent; // 指向父节点的指针,用于追踪路径
|
|
|
|
|
|
|
|
int f, g, h; // A*算法中的 f, g, h 值
|
|
|
|
|
|
|
|
} Node;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建新的拼图节点
|
|
|
|
|
|
|
|
Node* createNode(int puzzle[N][N]) {
|
|
|
|
|
|
|
|
Node* newnode = (Node*)malloc(sizeof(Node));
|
|
|
|
|
|
|
|
//请实现该函数
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < N; i++) {
|
|
|
|
|
|
|
|
for (j = 0; j < N; j++) {
|
|
|
|
|
|
|
|
newNode->puzzle[i][j] = puzzle[i][j];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
newNode->parent = NULL;
|
|
|
|
|
|
|
|
newNode->f = 0;
|
|
|
|
|
|
|
|
newNode->g = 0;
|
|
|
|
|
|
|
|
newNode->h = 0;
|
|
|
|
|
|
|
|
return newNode;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查两个拼图状态是否相同
|
|
|
|
|
|
|
|
bool isSamePuzzle(int a[N][N], int b[N][N]) {
|
|
|
|
|
|
|
|
//相同则返回true,否则返回false
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < N; i++) {
|
|
|
|
|
|
|
|
for (j = 0; j < N; j++) {
|
|
|
|
|
|
|
|
if (a[i][j] != b[i][j]) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|