セルのあらゆる方向にやみくもに実行して、ゴールが見つかったかどうかを確認する単純な迷路探索アルゴリズムを作成しました。ゴールを見つけることができますが、ゴールが見つかったときに他の再帰呼び出しを終了せず、ゴールへの可能なパスを 1 つだけではなく、行ったすべてのパスを描画するため、ソリューションはまだ悪いです。どうすればそれを改善できますか?
疑似コードの基本的なアルゴリズムは次のようになります。
function searchMaze(start_point, end_point, maze) {
// If current point is goal I would like to abort other recursive calls
if(start_point.equals(end_point)) {
pathFound = true;
drawPathInArray();
return;
}
else {
// if current point is not inside the array
if(start_point_not_within_array)
return
else {
// if current point is a wall or a cell that has already been visited
if(cell_is_wall || cell_is_visited)
return;
else {
// mark cell as possible path
markCellInPathArray("#");
setCellVisited();
searchMaze(left_from_start_point, end_point, maze);
searchMaze(right_from_start_point, end_point, maze);
searchMaze(above_start_point, end_point, maze);
searchMaze(below_start_point, end_point, maze);
}
}
}
}