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){
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);
//Pohyb dole
if(actualPos < (size*size-size) && maze[actualPos+size]==' '){
//printf("dole\n");
maze[actualPos+size]='*';
actualPos+=size;
continue;
}
//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;
}
//Pohyb vlavo
if(maze[actualPos-1]==' '){
//printf("vlavo\n");
maze[actualPos]='*';
actualPos--;
continue;
}
if(journey == size*size-1){
maze[journey]= '*';
return 1;
}
if(journey >= 0 && journey < (size*size) && maze[journey]==' '){
maze[journey] = '*';
//Pohyb dole
if(paths(maze, journey+size, size)){
return 1;
}
//Pohyb vpravo
if(paths(maze, journey+1, size)){
return 1;
}
return 1;
//Pohyb hore
if(paths(maze, journey-size, size)){
return 1;
}
//Pohyb vlavo
/* if(paths(maze, journey-1, size))
return 1;
*/
maze[journey] = ' ';
}
return 0;
}
int solve_maze(char* maze, int size){
return paths(maze, 0, size);
}