zmena strategie -> backtraking

This commit is contained in:
Valér Jakubčo 2022-01-22 00:24:40 +01:00
parent 05d668c669
commit 67fd7de2aa

View File

@ -4,63 +4,45 @@
#include <assert.h>
#include <math.h>
int paths(char* maze, int journey, int size){
if(journey == size*size-1){
maze[journey]= '*';
return 1;
}
int solve_maze(char* maze,int size){
//int end = size*size;
int actualPos = 0;
maze[actualPos]='*';
while(actualPos != size*size-1){
//printf("acpos: %d\n", actualPos);
if(journey >= 0 && journey < (size*size) && maze[journey]==' '){
maze[journey] = '*';
//Pohyb dole
if(actualPos < (size*size-size) && maze[actualPos+size]==' '){
//printf("dole\n");
maze[actualPos+size]='*';
actualPos+=size;
continue;
if(paths(maze, journey+size, size)){
return 1;
}
//Pohyb hore
if(actualPos-size > size && maze[actualPos-size]==' '){
//printf("hore\n");
maze[actualPos-size]='*';
actualPos-=size;
continue;
}else if(actualPos-size > size && maze[actualPos-size]=='*'){
maze[actualPos]=' ';
actualPos-=size;
}
//Pohyb vpravo
if(maze[actualPos+1]==' '){
//puts("vpravo");
maze[actualPos+1]='*';
actualPos++;
continue;
if(paths(maze, journey+1, size)){
return 1;
}
//Pohyb hore
if(paths(maze, journey-size, size)){
return 1;
}
//Pohyb vlavo
if(maze[actualPos-1]==' '){
//printf("vlavo\n");
maze[actualPos]='*';
actualPos--;
continue;
}
}
/* if(paths(maze, journey-1, size))
return 1;
*/
maze[journey] = ' ';
}
return 0;
}
int solve_maze(char* maze, int size){
return paths(maze, 0, size);
}