|
|
@ -16,25 +16,37 @@ Node* createNode(int puzzle[N][N]) {
|
|
|
|
Node* newnode = (Node*)malloc(sizeof(Node));
|
|
|
|
Node* newnode = (Node*)malloc(sizeof(Node));
|
|
|
|
//请实现该函数
|
|
|
|
//请实现该函数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < N; i++){
|
|
|
|
|
|
|
|
for(int j = 0; j < N; j++){
|
|
|
|
|
|
|
|
newnode.puzzle[i][j] = puzzle[i][j];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
newnode.parent = NULL;
|
|
|
|
|
|
|
|
newnode.f = 0;
|
|
|
|
|
|
|
|
newnode.g = 0;
|
|
|
|
|
|
|
|
newnode.h = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查两个拼图状态是否相同
|
|
|
|
// 检查两个拼图状态是否相同
|
|
|
|
bool isSamePuzzle(int a[N][N], int b[N][N]) {
|
|
|
|
bool isSamePuzzle(int a[N][N], int b[N][N]) {
|
|
|
|
//相同则返回true,否则返回false
|
|
|
|
//相同则返回true,否则返回false
|
|
|
|
|
|
|
|
for(int i = 0; i < N; i++){
|
|
|
|
|
|
|
|
for(int j = 0; j < N; j++){
|
|
|
|
|
|
|
|
if(a[i][j] != b[i][j]) return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 打印拼图状态
|
|
|
|
// 打印拼图状态
|
|
|
|
void printPuzzle(int puzzle[N][N]) {
|
|
|
|
void printPuzzle(int puzzle[N][N]) {
|
|
|
|
//双重for循环实现拼图的打印
|
|
|
|
//双重for循环实现拼图的打印
|
|
|
|
|
|
|
|
for(int i = 0; i < N; i++){
|
|
|
|
|
|
|
|
for(int j = 0; j < N; j++){
|
|
|
|
|
|
|
|
printf("%d ",puzzle[i][j]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -43,7 +55,12 @@ int heuristic(Node* current, Node* goal) {
|
|
|
|
int h = 0;
|
|
|
|
int h = 0;
|
|
|
|
// 计算不匹配的拼图块数量
|
|
|
|
// 计算不匹配的拼图块数量
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < N; i++){
|
|
|
|
|
|
|
|
for(int j = 0; j < N; j++){
|
|
|
|
|
|
|
|
if(current.puzzle[i][j] != goal.puzzle[i][j]) h++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 移动操作,生成新的拼图状态
|
|
|
|
// 移动操作,生成新的拼图状态
|
|
|
|