2022-01-19 22:23:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "maze.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
int issafe(char* maze, int x, int y, int size) {
|
2022-01-20 07:04:32 +00:00
|
|
|
if ((x >= 0 && x < size) && (y >= 0 && y < size) && (maze[x * size + y] == ' ')){
|
2022-01-19 22:23:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-20 07:04:32 +00:00
|
|
|
int backtracking(char* maze, int x, int y, int size){ //https://www.geeksforgeeks.org/rat-in-a-maze-backtracking-2/
|
|
|
|
if(x == size - 1 && y == size - 1 && maze[x * size + y] == ' '){ //kontrola ci sa nachadzam na konci
|
2022-01-19 22:23:25 +00:00
|
|
|
maze[x * size + y] = '*';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-01-20 07:04:32 +00:00
|
|
|
if(issafe(maze, x, y, size) == 1) { // znazornenie stien
|
2022-01-19 22:23:25 +00:00
|
|
|
maze[x * size + y] = '*';
|
|
|
|
|
2022-01-20 07:09:00 +00:00
|
|
|
if (backtracking(maze, x, y + 1, size) == 1) { //pohyb doprava
|
2022-01-19 22:23:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-01-20 07:09:00 +00:00
|
|
|
if (backtracking(maze, x + 1, y, size) == 1) { //pohyb dole
|
2022-01-19 22:23:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-01-20 07:26:50 +00:00
|
|
|
if (backtracking(maze, x - 1, y, size) == 1) { //pohyb dol
|
|
|
|
|
2022-01-19 22:23:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-01-20 07:09:00 +00:00
|
|
|
if (backtracking(maze, x, y - 1, size) == 1) { //pohyb hore
|
2022-01-19 22:23:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
maze[x * size + y] = ' ';
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int solve_maze(char* maze, int size) {
|
2022-01-20 07:09:00 +00:00
|
|
|
return backtracking(maze, 0, 0, size); //volanie funkcie
|
2022-01-19 22:23:25 +00:00
|
|
|
}
|