I got a mission to find the shortest path in maze 10X10. I defined the size by N and try at the beginning to debug with size 3X3 and 4X4 to check step by step,and the program worked fine. After this i tried to enlarge the size and then the program show me this and show as if stacked and don't completed the program (at the end i have to print the value of shortest path):
http://postimg.org/image/rsdzv3ugx/
this is my code. I don't know where is the problem. i don't know why 3X3 worked fine and bigger maze not.
#include <stdio.h>
#define N 7
#define UP 12
#define DOWN 20
#define RIGHT 30
#define LEFT 40
void paragraph_4 ();
int get2Dmatrix (int arr[N][N],int i,int j);
int maze (int maze_arr[N][N],int counter,int i,int j,int direction);
int min(int y1,int y2,int y3,int y4);
int main ()
{
// int arr[10][10]={0};
// get2Dmatrix (arr,0,0));
paragraph_4();
return 0;
}
void paragraph_4 ()
{
int maze_matrix[N][N];
printf("Please enter information for 10 by 10 matrix\n");
get2Dmatrix(maze_matrix,0,0);
int min_length=maze (maze_matrix,0,0,0,DOWN);
if (min_length==(N*N)+1)
printf("No path\n");
else
printf("The shortest path length is %d\n",min_length-1);
}
int get2Dmatrix (int arr[N][N],int i,int j)
{
if (j==N)
{
i++;
j=0;
}
if (i==N)
return arr [N][N];
scanf ("%d",&arr[i][j]);
get2Dmatrix (arr,i,j+1);
if (i==0 && j==0)
return arr [N][N];
}
int maze (int maze_arr[N][N],int counter,int i,int j,int direction)
{
int x1=(N*N)+1,x2=(N*N)+1,x3=(N*N)+1,x4=(N*N)+1;// initialization to impossible steps.
counter++;
if (i==(N-1) && j==(N-1))// stop recursion conditions
return counter;
if (counter==(N*N)+1)
return ((N*N)+1);
if ((i<(N-1)) && (direction!=UP) && (maze_arr[i+1][j]==1))// go down
x1=maze(maze_arr,counter,i+1,j,DOWN);
if ((i>0) && (direction!=DOWN) && (maze_arr[i-1][j]==1))//go up
x2=maze(maze_arr,counter,i-1,j,UP);
if ((j<(N-1)) && (direction != LEFT) && (maze_arr[i][j+1]==1))//go right
x3=maze(maze_arr,counter,i,j+1,RIGHT);
if ((j>0) && (direction!= RIGHT) && (maze_arr[i][j-1]==1))//go left
x4=maze(maze_arr,counter,i,j-1,LEFT);
return min(x1,x2,x3,x4);//send 4 numbers for function and return the
}
// Get 4 numbers and return the lowest value.
int min(int y1,int y2,int y3,int y4)
{
if(y1==y2 && y2==y3 && y3==y4)
return y1;
else if (y1<=y2 && y1<=y3 && y1<=y4)
return y1;
else if (y2<=y1 && y2<=y3 && y2<=y4)
return y2;
else if (y3<=y1 && y3<=y2 && y3<=y4)
return y3;
else if (y4<=y1 && y4<=y2 && y4<=y3)
return y4;
}