gpt4 book ai didi

c - C中的回溯迷宫求解器

转载 作者:太空宇宙 更新时间:2023-11-04 04:34:29 24 4
gpt4 key购买 nike

我正在为迷宫编写查找路径解决方案 [30][30]我在路径查找功能中使用了回溯;这是伪代码:

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

  1. 找到起点 [v,h] 然后为该点运行 find_path 函数
  2. 将房间标记为已访问
  3. 终止条件 1:如果 h=29,在路径上标记然后返回 true
  4. 递归部分:搜索 8 个邻居:west、northwest、north、northeast、east、southeast、south、southwest。检查房间是否可访问,然后调用 find_path 函数,如果返回 true,则标记查找路径。
  5. 终止条件 2:返回 false。

当我运行它时,它总是给出段错误。我尝试了不同的方法,但都给出了相同的错误?输出应该是这样的图像: 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;}

最佳答案

尝试使用这个

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;

已编辑:感谢@NiBZ - 它看起来有点乱,但现在不会导致段错误

关于c - C中的回溯迷宫求解器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32477653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com