所以我试图找出一个给定的迷宫从头到尾有多少种方式存在。
这是我的代码,使用深度优先搜索。
import java.util.Stack;
public class NumberofWaysInAMaze {
class VisitedNode
{
private int x;
private int y;
public VisitedNode(int x, int y)
{
this.x = x;
this.y = y;
}
}
public int findNumberofWaysToMaze(boolean[][] maze, int targetX, int targetY)
{
int numberofWays = 0;
int rows = maze.length;
int columns = maze[0].length;
boolean[][] visitedMap = new boolean[rows][columns];
for(int i=0; i < rows; i++)
{
System.arraycopy(maze[i], 0, visitedMap[i], 0, maze[i].length);
}
Stack<VisitedNode> stack = new Stack<VisitedNode>();
VisitedNode root = new VisitedNode(0, 0);
stack.push(root);
while(!stack.empty())
{
VisitedNode visitingNode = stack.pop();
int currentX = visitingNode.x;
int currentY = visitingNode.y;
if(currentX == targetX && currentY == targetY)
{
numberofWays++;
}else
{//System.out.println("x is:" + currentX + "--- y is:" + currentY);
visitedMap[currentX][currentY] = true;
}
if(currentX+1 <= targetX && currentX+1 >= 0 && visitedMap[currentX+1][currentY] == false && maze[currentX+1][currentY] == false)
{
stack.push(new VisitedNode(currentX+1, currentY));
}
if(currentX-1 <= targetX && currentX-1 >= 0 && visitedMap[currentX-1][currentY] == false && maze[currentX-1][currentY] == false)
{
stack.push(new VisitedNode(currentX-1, currentY));
}
if(currentY+1 <= targetY && currentY+1 >= 0 && visitedMap[currentX][currentY+1] == false && maze[currentX][currentY+1] == false)
{
stack.push(new VisitedNode(currentX, currentY+1));
}
if(currentY-1 <= targetY && currentY-1 >= 0 && visitedMap[currentX][currentY-1] == false && maze[currentX][currentY-1] == false)
{
stack.push(new VisitedNode(currentX, currentY-1));
}
}
return numberofWays;
}
public static void main(String[] args)
{
NumberofWaysInAMaze test = new NumberofWaysInAMaze();
boolean[][] maze =
{
{false, false, false, false, false},
{false, true, false, true, false},
{false, true, false, true, false},
{false, true, false, true, false},
{false, false, false, false, false},
};
System.out.println(test.findNumberofWaysToMaze(maze, 4, 4));
}
}
但是,输出不正确。我的输出是 2,显然,在给定的迷宫中有四种方式(false 表示可以通过,true 表示阻止)。我知道原因是因为我使用另一个二维数组来记录一个点是否被访问过。但是,我不知道如何修改我的解决方案。任何意见或建议将不胜感激。
谢谢