Message
Message
h>
#include <stdlib.h>
#include <string.h>
#define ROWS 3
#define COLS 3
if (newRow >= 0 && newRow < ROWS && newCol >= 0 && newCol < COLS) {
children[i] = *currentState;
children[i].puzzle[blankRow][blankCol] = currentState->puzzle[newRow]
[newCol];
children[i].puzzle[newRow][newCol] = 0;
}
}
}
if (isGoalState(¤t->state)) {
return current; // Solution found
}
PuzzleState children[4];
generateChildren(¤t->state, children);
int j;
for (j = 0; j < rear; ++j) {
if (isEqual(&queue[j]->state, &newNode->state)) {
free(newNode); // Skip already visited state
break;
}
}
if (j == rear) {
queue[rear++] = newNode;
}
}
}
int main() {
PuzzleState initialState;
printf("Enter the initial state of the puzzle (0 represents the blank tile):\
n");
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
scanf("%d", &initialState.puzzle[i][j]);
}
}
if (solutionNode != NULL) {
printf("Solution found:\n");
printSolutionReverse(solutionNode);
} else {
printf("No solution found.\n");
}
// Clean up memory
while (solutionNode != NULL) {
Node* temp = solutionNode;
solutionNode = solutionNode->parent;
free(temp);
}
return 0;
}