Find all possible paths and the shortest path through a maze?

Question

I'm trying to create a program that will traverse a randomly generated maze where 1's are open and 0's are walls. starting in the top left and ending in the bottom right. The path can go up, down, left, and right.

Currently, my program gives me a solution, but I'm having trouble getting it to print more than one path.

So if anyone can help me out to get it to print out more than one solution that would be awesome. Here's my code, I omitted the part where I randomly generate my maze.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

int  n, minMatrix, solIndex = 1, minLen = 10000000;


bool solveMaze(int **mat,int x, int y, int sol[n][n], int count){

int i, j;
  if((!(x >= 0 && x <n  && y >=0 && y < n)) || mat[x][y] == 0 || sol[x][y] == 1){
    return false;
  }
  
  if(x == n-1 && y == n-1){
    sol[x][y] = 1;
    
    printf("Solution %d is:\n", solIndex);
    for(i = 0; i < n; i++)
    {
      for( j=0;j<n;j++)
      {
        printf("%d", sol[i][j]);
      }
      printf("\n");
    }

    if(count<minLen)
    {
      minLen = count;
      minMatrix = solIndex;
    }
    solIndex +=1;
    sol[x][y] = 0;
    return true;
  }
  
  sol[x][y] = 1;
  
  if(solveMaze(mat, x+1, y, sol, count+1)){
    return true;
  }
 
  if(solveMaze(mat, x-1, y, sol, count+1)){
    return true;
  }

  if(solveMaze(mat, x, y+1, sol, count+1)){
    return true;
  }

  if(solveMaze(mat, x, y-1, sol, count+1)){
    return true;
  }
  sol[x][y] = 0;
  return false;
 
}

int main(){

if(!solveMaze(**mat, 0, 0, sol, 0)){
    printf("No possible paths, run program again\n");
  }
  else{
    printf("the shortest path is %d\n", minMatrix);
  }
}

Posted May 11, 2016, 12:56 AM UTC · edited

10
[deleted]•

[deleted]

1