Well, someone can help me. I need to implement a recursive function that gets a labyrinth and a current position.
The maze is given by a char array where 'X' represents a wall, '0' represents a path, 'E' represents the input, and 'S' represents the output. The goal is to print on the screen a valid path leading from the entrance to the exit, this path does not have to be the shortest, but it can not pass twice through the same house. The labyrinth is indexed by% w / w, where width and height are the number of houses on the X axis and the Y axis respectively, for example, the labyrinth below is width 5 and height 6. The entry is in the house (3, 0) and the exit in the house (2,5):
XOSXX OOOXX OXXXX OXXOX OXXOX OOOEX
An example of a workaround for this problem would be the following path: (0.4) (1.0) (1.0) (0, 1) (0, 2) (0, 3) (0.4) (1, 4) (1, 5) (2, 5)
My question is how can I be printing on the screen a path leading from the entrance to the exit. Code so far:
#include <stdio.h>
#include <stdlib.h>
void print_position(int x, int y);
void print_maze(char **maze, int largura, int altura);
int labirinto(int x_atual, int y_atual, char **maze, int largura, int
altura){
int imaior = x_atual;
int imenor = x_atual;
int ybaixo = y_atual;
int ycima = y_atual;
while ( imaior < largura - 1){
if(maze[x_atual + 1][y_atual] != 'X' && maze[x_atual + 1][y_atual] !=
'P'){
maze[x_atual][y_atual] = 'P';
x_atual += 1;
}
imaior++;
}
while(imenor > 0){
if(maze[x_atual - 1][y_atual] != 'X' && maze[x_atual -1][y_atual] !=
'P') {
maze[x_atual][y_atual] = 'P';
x_atual -= 1;
}
imenor--;
}
while(ycima > 0){
if(maze[x_atual][y_atual - 1] != 'X' && maze[x_atual][y_atual - 1] !=
'P'){
maze[x_atual][y_atual] = 'P';
y_atual -= 1;
}
ycima--;
}
while(ybaixo < altura - 1){
if(maze[x_atual][y_atual + 1] != 'X' && maze[x_atual][y_atual + 1] !=
'P'){
maze[x_atual][y_atual] = 'P';
y_atual += 1;
}
ybaixo++;
}
maze = labirinto(x_atual , y_atual,maze , largura , altura);
}
int main(void){
int largura, altura, x_saida, y_saida, x, y;
scanf("%d %d\n", &largura, &altura);
char ** a = malloc(largura * sizeof(char*));
for(x = 0; x < largura; x++){
a[x] = malloc(altura * sizeof(char));
}
for(y = altura - 1; y >= 0; y--){
for(x = 0; x < largura; x++){
a[x][y] = getchar();
if(a[x][y] == 'S'){
x_saida = x;
y_saida = y;
}
}
getchar(); //pegar a quebra de linha
}
print_maze(a, largura, altura);
//eu acredito que seja mais facil comecar a busca pela saida
labirinto(x_saida, y_saida, a, largura, altura);
printf("\n");
return 0;
}
void print_maze(char **maze, int largura, int altura){
int x, y;
for(y = altura - 1; y >= 0; y--){
for(x = 0; x < largura; x++){
printf("%c", maze[x][y]);
}
printf("\n");
}
}
void print_position(int x, int y){
printf("(%d, %d)", x, y);
}