I am writing simple slide puzzle (Loyd) solver. I want to solve any given solveable configuration of puzzle with dimensions 3 x 3 and more. However I got stuck with implementation of recursion. I have got tree and stack.
typedef struct Node{
int **array;
struct Node *left;
struct Node *right;
struct Node *up;
struct Node *down;
struct Node *parent;
}Node;
Node* newNode(Node *parent, int **array){
Node* u;
u = malloc(sizeof(Node));
if (u == NULL){
printf("OUT OF MEMORY!");
/* Free memory here.*/
exit(1);
}
u->array = array;
u->down = NULL;
u->right = NULL;
u->up = NULL;
u->left = NULL;
u->parent = parent;
return u;
}
void setLeftNode(Node *parent, Node *child) {
parent->left = child;
}
/* setRightNode,Up,Down...*/
int isLeftChild (Node *node){
if (node->parent == null) {return 0;}
else{
if (node->parent->left == node){return 1;}
else{return 0;}
}
}
/*is Right,Up,Down...*/
Stack has static size. Now I have method, which is finding solution. But I don't know how can I implement recursion and how can I store created nodes and find path. So far I have got:
int trySolvePuzzle(stack *z, stack *tz, int size_array, int limit){
int x,y;
int duplicate = 1;
Node *parent = NULL;
Node *helper = NULL;
returnPeak(z,&parent);
/* SOLVED?*/
if ((isSolved(parent->array, size_array)) == 1){return 1;}
x = findCoordinatesZeroX(parent->array,size_array);
y = findCoordinatesZeroY(parent->array,size_array);
if(y!=0){
helper = newNode(parent, toLeft(parent->array, size_array, x, y));
setLeftNode(parent, helper);
duplicate = searchForDuplicate(z, tz, helper, size_array);
}
else{
/*????*/
}
/* It is NOT in PATH*/
if (duplicate == 0){
push(z, &helper);
if (trySolvePuzzle(z, tz, size_array, limit+1) == 1){
return 1;
}
}
/*BACKTRACK*/
pop(z, &parent);
x = findCoordinatesZeroX(parent->array,size_array);
y = findCoordinatesZeroY(parent->array,size_array);
if (y!=size_array-1){
helper = newNode(parent, toRight(parent->array, size_array, x, y));
setRightNode(parent, helper);
duplicate = searchForDuplicate(z, tz, helper, size_array);
}
else{
/*????*/
}
if (duplicate == 0){
push(z, &helper);
if (trySolvePuzzle(z, tz, size_array, limit+1) == 1){
return 1;
}}
pop(z, &parent);
x = findCoordinatesZeroX(parent->array,size_array);
y = findCoordinatesZeroY(parent->array,size_array);
if (x != 0){
helper = newNode(parent, toUp(parent->array, size_array, x, y));
setUpNode(parent, helper);
duplicate = searchForDuplicate(z, tz, helper, size_array);}
else{
/*????*/
}
if (duplicate == 0){
push(z, &helper);
if (trySolvePuzzle(z, tz, size_array, limit+1) == 1){
return 1;
}
}
pop(z, &parent);
x = findCoordinatesZeroX(parent->array,size_array);
y = findCoordinatesZeroY(parent->array,size_array);
if (x!=size_array-1){
helper = newNode(parent, toDown(parent->array, size_array, x, y));
setDownNode(parent, helper);
duplicate = searchForDuplicate(z, tz, helper, size_array);}else{
/* ???? */
}
if (duplicate == 0){
push(z, &helper);
if (trySolvePuzzle(z, tz, size_array, limit+1) == 1){
return 1;
}}
pop(z, &parent);
/*CAN NOT CONTINUE*/
return 0;
}
So what I should do, when for example. y = 0, so I can't create new left configuration. I need to go back. But when I do I stuck in infinite loop. Please note, that I want to solve any size of puzzle > 2.
And here is my function to search for duplicate in stack:
int searchForDuplicate(stack *z, stack *tz, Node *helper, int size_array){
int duplicate = 1;
int q,w = 0;
Node *temp = NULL;
while ((pop(z, &temp))==1){
//pop(z, &temp);
for (q = 0; q < size_array; q++){
for (w = 0; w < size_array; w++){
if (temp->array[q][w]!= helper->array[q][w]){duplicate = 0;}
}
}
push(tz, &temp);
}
while ((pop(tz, &temp))==1){
//pop(tz, &temp);
push(z, &temp);
}
return duplicate;
}
Anyone can help? Thank you for your time. :)