usaa21/sk1a/maze.c

89 lines
2.5 KiB
C
Raw Normal View History

2021-12-28 15:21:01 +00:00
#include <stdio.h>
#include <string.h>
#include "maze.h"
#include <assert.h>
2022-01-03 14:54:09 +00:00
int solve_maze(char* maze,int size) {
int i = 0;
2022-01-03 17:28:22 +00:00
int p, l, h, d = 0;
2022-01-03 14:54:09 +00:00
while(i != size*size-1){
2022-01-05 11:46:14 +00:00
//if (i % size - 1 != 0){l = 1;}
2022-01-05 11:30:37 +00:00
//else if (i < size*size - size){h = 1;}
//else if (i % size){p = 1;}
//else if (i >= size){d =1;}
2022-01-05 11:46:14 +00:00
if (maze[i + 1] == ' ' /*&& i % size - 1 != 0 */&& l != 1) {
2022-01-03 14:54:09 +00:00
maze[i + 1] = '*';
i++;
2022-01-03 17:28:22 +00:00
p = 0;
l = 0;
h = 0;
d = 0;
2022-01-05 11:46:14 +00:00
} else if (maze[i + size] == ' ' /*&& i < (size*size - size) != 0 */&& h != 1) {
2022-01-03 14:54:09 +00:00
maze[i + size] = '*';
i = i + size;
2022-01-03 17:28:22 +00:00
p = 0;
l = 0;
h = 0;
d = 0;
2022-01-05 11:46:14 +00:00
} else if (maze[i - 1] == ' ' /*&& i % size != 0 */&& p != 1) {
2022-01-03 14:54:09 +00:00
maze[i - 1] = '*';
i--;
2022-01-03 17:28:22 +00:00
p = 0;
l = 0;
h = 0;
d = 0;
2022-01-05 11:46:14 +00:00
} else if (maze[i - size] == ' ' /*&& i >= size */&& d != 1) {
2022-01-03 14:54:09 +00:00
maze[i - size] = '*';
i = i - size;
2022-01-03 17:28:22 +00:00
p = 0;
l = 0;
h = 0;
d = 0;
2022-01-03 14:54:09 +00:00
}
2022-01-03 17:28:22 +00:00
else{
while(1){
2022-01-03 14:54:09 +00:00
2022-01-03 17:28:22 +00:00
if (maze[i + 1] == ' ' && i != size - 1 && l != 1) {
2022-01-04 19:39:32 +00:00
//i++;
2022-01-03 17:28:22 +00:00
break;
}
if (maze[i + size] == ' ' && i != size - 1 && h != 1) {
2022-01-04 19:39:32 +00:00
//i = i + size;
2022-01-03 17:28:22 +00:00
break;
}
if (maze[i - 1] == ' ' && i != 0 && p != 1) {
2022-01-04 19:39:32 +00:00
//i--;
2022-01-03 17:28:22 +00:00
break;
}
if (maze[i - size] == ' ' && i > size && d != 1) {
2022-01-04 19:39:32 +00:00
//i = i - size;
2022-01-03 17:28:22 +00:00
break;
}
p = 0;
l = 0;
h = 0;
d = 0;
if (maze[i + 1] == '*' && i != size - 1) {
maze[i] = ' ';
i++;
p = 1;
} else if (maze[i + size] == '*' && i != size - 1) {
maze[i] = ' ';
i = i + size;
d = 1;
} else if (maze[i - 1] == '*' && i != 0) {
maze[i] = ' ';
i--;
l = 1;
} else if (maze[i - size] == '*' && i > size) {
maze[i] = ' ';
i = i - size;
h = 1;
}
}
}
}
2022-01-05 11:32:42 +00:00
maze[0] = '*';
2022-01-03 14:54:09 +00:00
return 1;
2021-12-28 15:21:01 +00:00
}