最も簡単なアルゴリズムを使用していくつかの迷路を生成したかっただけですが、すべての迷路は次のようになります。
以下は Java コードの一部です (whatVisit 関数は正しく動作します。見ないでください)。
private void dfs(Point start, boolean[][] visited) {
Point nextCell = whatVisit(start, visited);
if(nextCell == null) // if there's nothing to visit
return;
// mark current cell as visited
visited[start.y][start.x] = true;
// destroy the wall between current cell and the new one
borders[(start.y + nextCell.y)/2][(start.x + nextCell.x)/2] = true;
// start a new search from found cell
dfs(nextCell, visited);
}
private Point whatVisit(Point p, boolean[][] visited) {
Vector<Point>cells = new Vector<Point>(); // to store acessible cells
// lookaround
if(p.x - 2 >= 0 && !visited[p.y][p.x - 2])
cells.add(new Point(p.x - 2, p.y));
if(p.x + 2 < visited[0].length && !visited[p.y][p.x + 2])
cells.add(new Point(p.x + 2, p.y));
if(p.y - 2 >= 0 && !visited[p.y - 2][p.x])
cells.add(new Point(p.x, p.y - 2));
if(p.y + 2 < visited.length && !visited[p.y + 2][p.x])
cells.add(new Point(p.x, p.y + 2));
// instead of Random
Collections.shuffle(cells);
// returns null if there are no acessible cells around
if(cells.size() > 0)
return cells.get(0);
else return null;
}
そして、私はそれがうまくいかない理由を知っています!DFS が最終的にアクセス可能なセルが存在しない場所に到達すると、DFS は開始に戻ります。
これを修正して強制的に正しく動作させるにはどうすればよいですか?
ありがとう。