I want to write a code to find a shortest path in the labyrinth. I wrote this;
#include <stdio.h>
void change (void);
void print(void);
int labirent[13][13] = {
{1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,2,0,1,0,0,0,0,0,0,0,0,1},
{1,0,0,1,0,0,1,0,1,1,1,1,1},
{1,0,0,1,0,0,1,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,0,0,1},
{1,0,0,0,0,1,1,1,1,0,0,0,1},
{1,0,1,1,1,1,1,0,0,0,0,0,1},
{1,0,0,0,0,1,0,0,0,1,1,1,1},
{1,0,0,0,0,1,0,0,0,0,0,0,1},
{1,1,1,1,0,1,0,0,1,1,0,0,1},
{1,0,0,0,0,1,0,0,1,0,0,1,1},
{1,0,0,0,0,0,0,0,1,0,0,3,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1}
};//defining labyrinth - walls are (1), starting point is (2), null points are (0), end point is (3)
int i = 3;
int a = 0;
int x=1, y=1;//current positions
int main(){
change();
print();
return 0;
}
void change(){
if(labirent[x+1][y]==0){
labirent[x+1][y]=i;
x = x + 1;
change();
}else if(labirent[x][y+1]==0){
labirent[x][y+1]=i;
y = y+1;
change();
}else if(labirent[x-1][y]==0){
labirent[x-1][y]=i;
x = x-1;
change();
}else if(labirent[x][y-1]==0){
labirent[x][y-1]=i;
y = y -1;
change();
}
}
void print(){
for(int i=0;i<13;i++){
for(int j=0;j<13;j++){
printf("%d ",labirent[i][j]);
}
printf("\n");
}
}
It works for this path, but how can i make a more general one? I could not understand stacks. I might use it if you can show me the way.
Edit: End point is added.
Thank you already.