Well, there are some issues with your code, can you try below code to fix this error.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define N 3
typedef struct {
int puzzle[N][N];
int g, h;
int f;
} Node;
Node* createNode(int puzzle[N][N]) {
Node* node = (Node*)malloc(sizeof(Node));
memcpy(node->puzzle, puzzle, sizeof(node->puzzle[0][0]) * N * N);
node->g = 0;
node->h = 0;
node->f = 0;
return node;
}
int isGoal(Node* node, int goal[N][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (node->puzzle[i][j] != goal[i][j]) {
return 0;
}
}
}
return 1;
}
int getEmptyIndex(Node* node) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (node->puzzle[i][j] == 0) {
return i * N + j;
}
}
}
return -1;
}
int heuristic(Node* node, int goal[N][N]) {
int manhattanDistance = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int val = node->puzzle[i][j];
if (val != 0) {
int goalX = (val - 1) / N;
int goalY = (val - 1) % N;
manhattanDistance += abs(i - goalX) + abs(j - goalY);
}
}
}
return manhattanDistance;
}
Node* getNextNode(Node* node, int goal[N][N]) {
int moves[4] = {0, 0, 0, 0}; // Adjust this for your puzzle.
int emptyIndex = getEmptyIndex(node);
int i, j;
int minF = INT_MAX;
Node* nextNode = NULL;
for (i = 0; i < 4; i++) {
int newIndex = moves[i];
if (newIndex != -1) {
Node* newNode = createNode(node->puzzle);
// Implement the puzzle move logic here.
newNode->g = node->g + 1;
newNode->h = heuristic(newNode, goal);
newNode->f = newNode->g + newNode->h;
if (newNode->f < minF) {
minF = newNode->f;
nextNode = newNode;
}
}
}
return nextNode;
}
void printPuzzle(Node* node) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", node->puzzle[i][j]);
}
printf("\n");
}
}
int main() {
int puzzle[N][N] = {
{2, 1, 8},
{6, 4, 3},
{7, 0, 5}
};
int goal[N][N] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};
Node* startNode = createNode(puzzle);
Node* currentNode = startNode;
while (!isGoal(currentNode, goal)) {
currentNode = getNextNode(currentNode, goal);
if (currentNode == NULL) {
printf("No solution found.\n");
break;
}
printPuzzle(currentNode);
}
// Clean up and free memory as needed.
}
I hope you can fix your error.