Skip to content

Instantly share code, notes, and snippets.

@shrujandev
Last active November 19, 2021 03:42
Show Gist options
  • Save shrujandev/a97a286f168bb90d9ff961859254ffd0 to your computer and use it in GitHub Desktop.
Save shrujandev/a97a286f168bb90d9ff961859254ffd0 to your computer and use it in GitHub Desktop.
Path finding Code for Rat in a Maze
void findPath(Head * ptr, int endI, int endJ, FILE * output) {
Stack * stack; //struct containg a node and pointer to the same type
int condition = 0;
stack = (Stack * ) malloc(sizeof(Stack)); //Mallocing the stack
stack -> head = NULL;
Node * move; //This pointer is the one that moves and finds path
move = ptr -> head;
pushElement(stack, move);
while (condition == 0) {
if (move -> i < endI && move -> j < endJ) {
if (move -> right -> data == 0) {
move = move -> right;
pushElement(stack, move);
}
else if (move -> down -> data == 0) {
move = move -> down;
pushElement(stack, move);
}
else if (move -> down -> data != 0 && move -> right -> data != 0) {
move -> data = 1;
move = popElement(stack);
}
}
else if (move -> i == endI && move -> j != endJ) {
if (move -> right -> data == 0) {
move = move -> right;
pushElement(stack, move);
}
else if (move -> down -> data != 0 && move -> right -> data != 0) {
move -> data = 1;
move = popElement(stack);
}
}
else if (move -> i != endI && move -> j == endJ) {
if (move -> down -> data == 0) {
move = move -> down;
pushElement(stack, move);
}
else if (move -> down -> data != 0 && move -> right -> data != 0) {
move -> data = 1;
move = popElement(stack);
}
}
else {
condition = 1;
}
}
@shrujandev
Copy link
Author

This is the path finding function that is present in the Rat-in-the-Maze repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment