-5

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;
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
user3205436
  • 57
  • 1
  • 8

1 Answers1

0
  1. Access outside array size. Also: Since the return value of get2Dmatrix() is not used, simply return.

    int get2Dmatrix (int arr[N][N],int i,int j) {
      ...
      if (i==N) return arr [N][N];  // access arr [N][N] is UB
    }
    
    void get2Dmatrix (int arr[N][N],int i,int j) {
      ...
      if (i==N) return;
    }
    
  2. Of course, #define N 7 should be #define N 10.

  3. The stack size may be insufficient, but I don't think so. Try temporarily static int maze_matrix[N][N]; to move this off the stack.

  4. Minor: if(y1==y2 && y2==y3 && y3==y4) return y1; is not needed as this condition is caught in the next if (y1<=y2 && y1<=y3 && y1<=y4).

  5. Check scanf() result.

    if (scanf ("%d",&arr[i][j]) != 1) Alert_BadInput();
    
  6. With all this recursion, surprised you did not use something like:

    int min4(int y1,int y2,int y3,int y4) {
       return min2((y1 <= y2) ? y1 : y2, (y3 <= y4) ? y3 : y4);
    }
    int min2(int y1,int y2) {
       return (y1 <= y2) ? y1 : y2;
    }
    
  7. Minor:

    // printf("Please enter information for 10 by 10 matrix\n");
    printf("Please enter information for %d by %d matrix\n", N, N);
    
  8. All in all, aside from UB, stack, and IO error, I do not yet see the smoking gun.

  9. Hmmm. I'm thinking int min_length=maze (maze_matrix,0,0,0,DOWN); should start with (maze_matrix,0,0,0, TBD); where TBD maybe should be 0.

  10. Further: I think I have it. Code uses direction to insure the walking of the maze does not go back to "whence it came". But this does not prevent a circling: UP, RIGHT DOWN, LEFT, UP, RIGHT, ... To fix, code could leave a marker for the spaces visited and use that to prevent looping.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256