1

I am writing the find path solution for maze [30][30] I used backtracking in path find function; Here is pseudo code:

bool find_path(myMap,myVisited,myPath,v,h)

  1. find starting point [v,h] then run find_path function for that point
  2. mark the room as visited
  3. terminate condition 1: if h=29, mark on path then return true
  4. recursion part: search for 8 neighbors: west,northwest,north,northeast,east,southeast, south,southwest. check if the room is accessible, then call find_path function, if it return true, mark on find path.
  5. terminate condition 2: return false.

When I run it, it always give segmentation fault. I had tried different method but all give same errors? the output should be like this image: http://postimg.org/image/cd8unwet5/ here is my code:

// myMap ='.' is a block on map
//myVisited ='*' room visited
bool isSafe(char myMap[30][30], char myVisited[30][30], int v,int h){
if(v>=0||v<30||h>=0||h<30||myMap[v][h]!='.'||myVisited[v][h]=='*')return true;
    return false;}

//3 map
//myMap contain the maze
//myVisited use to mark room visited
//myPath is final path.
bool find_path(char myMap[30][30],char myVisited[30][30],char myPath[30][30], int v,int h){
//giving h=-1 and v=-1 , find starting point.
    if(h==-1&&v==-1){
        h=h+1;
        for (v=0;v<30;v++){
        if(myMap[v][h]!='.'&& h==0) {find_path(myMap,myVisited,myPath,v,h);}
        }

    }

    myVisited[v][h]='*';    //mark room as visited.
    if(h==29){              //stop when column is 29 and mark on path
        myPath[v][h]='*';
        return true;}

    if(isSafe(myMap,myVisited,v,h-1)==true){                //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v,h-1)==true){  // there is way out 
        myPath[v][h]='*';           //mark on myPath
        }
    }
    //.......same code for other room

    if(isSafe(myMap,myVisited,v,h+1)==true){                //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v,h+1)==true){  // there is way out 
        myPath[v][h]='*';           //mark on myPath
        }
    }   
    if(isSafe(myMap,myVisited,v-1,h)==true){                //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v-1,h)==true){  // there is way out 
        myPath[v][h]='*';           //mark on myPath
        }
    }
    if(isSafe(myMap,myVisited,v+1,h)==true){                //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v+1,h)==true){  // there is way out 
        myPath[v][h]='*';           //mark on myPath
        }
    }
    if(isSafe(myMap,myVisited,v+1,h-1)==true){              //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v,h-1)==true){  // there is way out 
        myPath[v][h]='*';           //mark on myPath
        }
    }
        if(isSafe(myMap,myVisited,v-1,h-1)==true){              //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v-1,h-1)==true){    // there is way out 
        myPath[v][h]='*';           //mark on myPath
        }
    }
            if(isSafe(myMap,myVisited,v-1,h+1)==true){              //if room is okie to access
        if(find_path(myMap,myVisited,myPath,v-1,h+1)==true){    // there is way out 
        myPath[v][h]='*';           //mark on myPath
        }
    }
    myVisited[v][h]='.';
        return false;//back track 
return false;}
M Oehm
  • 28,726
  • 3
  • 31
  • 42
Taylor
  • 11
  • 3
  • 1
    Run your code through a debugger, you should find the error. – Haris Sep 09 '15 at 10:58
  • Try to run it with a small maze of 3 X 3, does it work ? Try to debug it. What line crushes ? – OopsUser Sep 09 '15 at 11:00
  • 2
    `if(v>=0||v<30||h>=0||h<30||myMap[v][h]!='.'||myVisited[v][h]=='*')return true;` You are confusing and and or here. – wildplasser Sep 09 '15 at 11:05
  • 1
    You are using the parameter `v` as a loop variable and later treat it as input again, when it might hold an index beyond the end of the board. Recipe for disaster. – M Oehm Sep 09 '15 at 11:27
  • I have fixed isSafe as Itay suggest but, the map print out only starting point and nothing else. Does something wrong with my find_path???? – Taylor Sep 09 '15 at 11:28

1 Answers1

0

try using this

bool isSafe( char myMap[ 30 ][ 30 ], char myVisited[ 30 ][ 30 ], int v,int h ) {
    bool ret_val = true;

    ret_val = ( v >= 0 ) && ( v < 30 ) && ( h >= 0 ) && ( h > 30 ) && ( myMap[ v ][ h ] != '.' ) && ( myVisited[ v ][ h ] != '*' );

    return ret_val;

}

EDITED: thanks @NiBZ - it seems a bit more messy but it won't cause seg faults now

Itay Sela
  • 942
  • 9
  • 26
  • thank, It has no more segmentation error but the map only print the starting point , nothing else.Does something wrong with my find_path function – Taylor Sep 09 '15 at 11:27
  • 1
    In this case, the `myMap[v][h]` and `myVisited[v][h]` will always be evaluated, even when `v` or `h` is out of range. A one liner (`bool ret_val = (v>=0) && (v<30) ... && (myVisited[v][h] == '*');`) will stop evaluation at the first false condition. – NiBZ Sep 09 '15 at 13:09
  • Do you know anything wrong with my find_path function?? it seemed not working???? – Taylor Sep 09 '15 at 13:57
  • i think you ment it to be `myVisited[ v ][ h ] != '*'` – Itay Sela Sep 09 '15 at 14:14