मैं सिर्फ कुछ आसान कलन विधि का उपयोग mazes उत्पन्न करने के लिए चाहता था, लेकिन मेरे सभी mazes के बाद एक की तरह लग रहे:भूलभुलैया डीएफएस का उपयोग कर पीढ़ी विफल रहता है और मैं नहीं जानता कि क्यों
यहाँ एक टुकड़ा है जावा कोड का (एक 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;
}
और मुझे पता है क्यों यह काम नहीं करता! जब डीएफएस अंततः उस स्थान पर आती है जहां कोई सुलभ कोशिकाएं नहीं होती हैं, तो यह अभी शुरू होने के लिए बाहर जा रही है।
इसे कैसे ठीक करें और सही काम करने के लिए मजबूर करें?
धन्यवाद।
, क्या आप ऐसा करने के लिए जहां कोई सुलभ कोशिकाओं देखते हैं जब डीएफएस जगह की बात आती है चाहते हैं? मुझे लगता है, मेरा खुद का झुकाव पहले से बनाए गए पथ/पथ में कहीं और पथ खोज शुरू करने का प्रयास कर सकता है, और शायद प्रवेश द्वार और बाहर निकलें। –