简体   繁体   English

在C中回溯迷宫求解器

[英]Backtracking maze solver in C

I am writing the find path solution for maze [30][30] I used backtracking in path find function; 我正在为迷宫[30] [30]编写查找路径解决方案。在路径查找功能中使用了回溯; 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 找到起点[v,h]然后对该点运行find_path函数
  2. mark the room as visited 将房间标记为已访问
  3. terminate condition 1: if h=29 , mark on path then return true 终止条件1:如果h=29 ,在路径上标记,然后返回true
  4. recursion part: search for 8 neighbors: west,northwest,north,northeast,east,southeast, south,southwest. 递归部分:搜索8个邻居:西,西北,北,东北,东,东南,南,西南。 check if the room is accessible, then call find_path function, if it return true, mark on find path. 检查房间是否可访问,然后调用find_path函数,如果返回true,则在find路径上标记。
  5. terminate condition 2: return false. 终止条件2:返回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: 输出应如下图所示: http : //postimg.org/image/cd8unwet5/这是我的代码:

// 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;}

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 编辑:谢谢@NiBZ-似乎更混乱,但现在不会导致段错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM