I am trying to solve the maze using DFS algorithm.ORDER: West- North-South-East My output works in this logic Labyrinth picture1. It should not go up after the point (3,3),because the order of priority is west. What should I do?
#include <stdio.h>
char matrix[8][8];
void File()
{
int i,j;
FILE *file1;
file1=fopen("maze1.txt","r");
for(i=0; i<8; i++){
for(j=0;j<8;j++)
{
fscanf (file1, " %c", &matrix[i][j]);
printf("%c ",matrix[i][j]);
}
printf("\n");
}
}
void Print()
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
printf("%c ",matrix[i][j]);
}
printf("\n");
}
}
void stepDFS()
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
if(matrix[i][j] == 's')
{
DFS(i,j);
}
}
printf("\n");
}
}
DFS(int i,int j)
{
if(matrix[i][j-1] == '-')
{
matrix[i][j-1] = '.';
DFS(i,j-1);
}
if(matrix[i-1][j] == '-')
{
matrix[i-1][j] = '.';
DFS(i-1,j);
}
if(matrix[i+1][j] == '-')
{
matrix[i+1][j] = '.';
DFS(i+1,j);
}
if(matrix[i][j+1] == '-')
{
matrix[i][j+1] = '.';
DFS(i,j+1);
}
}
int main()
{
File();
Print();
stepDFS();
Print();
return 0;
}
maze1.txt ( # wall - null . step)
# # # # # # # #
# g - - # - - #
# # # - - - # #
# - # # - - - #
# - - # - - - #
# - - - - # # #
# - - # - - s #
# # # # # # # #
output
# # # # # # # #
# g . . # . . #
# # # . . . # #
# . # # . . . #
# . . # . . . #
# . . . . # # #
# . . # . . s #
# # # # # # # #
I don't know if this should be the output
# # # # # # # #
# g . . # - - #
# # # . . - # #
# . # # . - - #
# . . # . - - #
# . . . . # # #
# - - # . . s #
# # # # # # # #
--------------UPDATE-------------- STEP BY STEP
#include <stdio.h>
char matrix[8][8];
void ReadFile()
{
printf("\n\n\n START MAZE ");
int x,y;
FILE *file1;
file1=fopen("myMaze.txt","r");
for(x=0; x<8; x++){
for(y=0;y<8;y++)
{
fscanf (file1, " %c", &matrix[x][y]);
}
printf("\n");
}
}
void PrintMaze()
{
int x,y;
for(x=0;x<8;x++)
{
printf(" ");
for(y=0;y<8;y++)
{
printf(" %c ",matrix[x][y]);
}
printf("\n");
}
printf("\n\n\n\n");
}
int DFS(int x,int y){
if(matrix[x][y]=='g') // goal
{
return 1;
}
if(matrix[x][y-1] == '-' ){ //WEST
matrix[x][y-1] = '.';
PrintMaze();
if(DFS(x,y-1)){
matrix[x][y-1] ='.';
PrintMaze();
return 1;
}
else{
DFS(x,y-1);
return 0;
}
DFS(x,y-1);
}
if(matrix[x-1][y] == '-'){ //NORTH
matrix[x-1][y] = '.';
PrintMaze();
if(DFS(x-1,y)){
matrix[x-1][y] = '.';
PrintMaze();
return 1;
}
else{
DFS(x-1,y);
return 0;
}
DFS(x-1,y);
}
if(matrix[x+1][y] == '-'){ //SOUTH
matrix[x+1][y] = '.';
PrintMaze();
if(DFS(x+1,y)){
matrix[x+1][y] ='.';
PrintMaze();
return 1;
}
else{
DFS(x+1,y);
return 0;
}
DFS(x+1,y);
}
if(matrix[x][y+1] == '-'){ //EAST
matrix[x][y+1] = '.';
if(DFS(x,y+1)){
matrix[x][y+1] = '.';
PrintMaze();
return 1;
}
else{
DFS(x,y+1);
return 0;
}
DFS(x,y+1);
}
return 0;
}
void StartSearch()
{
printf(" STEP BY STEP");
int x,y;
for(x=0;x<8;x++)
{
for(y=0;y<8;y++)
{
if(matrix[x][y] == 's')//start
{
DFS(x,y);
}
}
printf("\n");
}
}
int main()
{
ReadFile();
PrintMaze();
StartSearch();
PrintMaze();
return 0;
}
myMaze.txt
# # # # # # # #
# g - - # - - #
# # # - - - # #
# - # # - - - #
# - - # - - - #
# - - - - # # #
# - - # - - s #
# # # # # # # #
DFS determines direction based on first if condition inside the function.Other directions should also be considered.