2021-12-28 15:21:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "maze.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
2022-01-19 21:29:10 +00:00
|
|
|
int issafe(char* maze, int x, int y, int size) {
|
|
|
|
if ((x >= 0 && x < size) && (y >= 0 && y < size) && maze[x * size + y] == ' '){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int backtracking(char* maze, int x, int y, int size){
|
|
|
|
if(x == size - 1 && y == size - 1 && maze[x * size + y] == ' '){
|
|
|
|
maze[x * size + y] = '*';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(issafe(maze, x, y, size) == 1) {
|
|
|
|
maze[x * size + y] = '*';
|
|
|
|
|
|
|
|
if (backtracking(maze, x, y + 1, size) == 1) {
|
|
|
|
return 1;
|
2022-01-03 14:54:09 +00:00
|
|
|
}
|
2022-01-19 21:29:10 +00:00
|
|
|
if (backtracking(maze, x + 1, y, size) == 1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (backtracking(maze, x - 1, y, size) == 1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (backtracking(maze, x, y - 1, size) == 1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
maze[x * size + y] = ' ';
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int solve_maze(char* maze, int size) {
|
|
|
|
if (maze[0] == '*'){
|
|
|
|
assert(maze[0] == '*');
|
|
|
|
maze[0] = ' ';
|
|
|
|
}
|
|
|
|
int x,y = 0;
|
|
|
|
while(x * size + y <= size*size - 1 ){
|
|
|
|
printf("%d\n%d", x * size + y, y);
|
|
|
|
//issafe(maze, x, y, size);
|
|
|
|
if (maze[x * size + y + 1] == ' ' /*&& y != 5*/){
|
|
|
|
assert(maze[x * size + y + 1] == ' ');
|
|
|
|
maze[x * size + y + 1] = '*';
|
|
|
|
y++;
|
2022-01-03 14:54:09 +00:00
|
|
|
|
2022-01-19 21:29:10 +00:00
|
|
|
}
|
|
|
|
if (maze[(x + 1) * size + y] == ' ' /*&& x != 5*/){
|
|
|
|
assert(maze[(x + 1) * size + y] == ' ');
|
|
|
|
maze[(x + 1) * size + y] = '*';
|
|
|
|
x++;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (maze[x * size + y - 1] == ' ' && y != 0){
|
|
|
|
assert(maze[x * size + y - 1] == ' ');
|
|
|
|
maze[x * size + y - 1] = '*';
|
|
|
|
y--;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (maze[(x - 1) * size + y] == ' ' /*&& x != 0*/){
|
|
|
|
assert(maze[(x - 1) * size + y] == ' ');
|
|
|
|
maze[(x - 1) * size + y] = '*';
|
|
|
|
x--;
|
|
|
|
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
backtracking(maze,x , y, size);
|
2022-01-03 17:28:22 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-19 21:29:10 +00:00
|
|
|
return 1;
|
2021-12-28 15:21:01 +00:00
|
|
|
}
|