gpt4 book ai didi

java - 0,1矩阵中的递归路径查找(并保存所有可能的路径)java

转载 作者:行者123 更新时间:2023-11-30 06:58:00 34 4
gpt4 key购买 nike

我正在尝试编写一个递归方法,该方法将找到一条路径,而无需回溯到 int 矩阵中包含值 0,1 的位置。 0 可以踩,1 不行。我还限制了一条超过 50 次移动的路径。

Location 是一个具有行值和列值 (x,y) 的对象。locationEquals 是一个函数,如果两个位置相同则返回 true,否则返回 false。 map 变量是我试图在其中查找路径的矩阵。

private static List<List<Location>> options = new ArrayList<List<Location>>();
public static void PathFind(List<Location> path)
{
Location current = path.get(path.size() - 1);
boolean done = false;
if(locationEquals(current,new Location(24,38)))
{
options.add(path);
return;
}
if(path.size() > 50) done = true;
if(!done)
{
try
{
if(map[current.row][current.col + 1] == 0)
{
if(!path.contains(new Location(current.row, current.col + 1)))
{
List<Location> temp = path;
temp.add(new Location(current.row, current.col + 1));
PathFind(temp);
}
}
}
catch (Exception e){}
try
{
if(map[current.row - 1][current.col] == 0)
{
if(!path.contains(new Location(current.row - 1, current.col)))
{
List<Location> temp = path;
temp.add(new Location(current.row - 1, current.col));
PathFind(temp);
}
}
}
catch (Exception e){}
try
{
if(map[current.row][current.col - 1] == 0)
{
if(!path.contains(new Location(current.row, current.col - 1)))
{
List<Location> temp = path;
temp.add(new Location(current.row, current.col - 1));
PathFind(temp);
}
}
}
catch (Exception e){}
try
{
if(map[current.row + 1][current.col] == 0)
{
if(!path.contains(new Location(current.row + 1, current.col)))
{
List<Location> temp = path;
temp.add(new Location(current.row + 1, current.col));
PathFind(temp);
}
}
}
catch (Exception e){}
}

执行以下代码后'options'为空,这意味着它没有找到方法。但这个矩阵中肯定有一种方法,所以这是我的代码中我找不到的错误。

最佳答案

问题是您每次进入递归的下一步时都不会创建一个新列表(您的 temp 变量并不是真正临时的,因为它只是对您的路径的引用,而不是它的副本)。

为了解决这个问题,我替换了 List<Location> temp = path;List<Location> temp = new ArrayList<>(path);

所以代码是:

private static List<List<Location>> options = new ArrayList<List<Location>>();
public static void PathFind(List<Location> path) {
Location current = path.get(path.size() - 1);
boolean done = false;
if (locationEquals(current, new Location(24, 38))) {
options.add(path);
return;
}
if (path.size() > 50) done = true;
if (!done) {
try {
if (map[current.row][current.col + 1] == 0) {
if (!path.contains(new Location(current.row, current.col + 1))) {
List<Location> temp = new ArrayList<>(path);
temp.add(new Location(current.row, current.col + 1));
PathFind(temp);
}
}
} catch (Exception e) {
}
try {
if (map[current.row - 1][current.col] == 0) {
if (!path.contains(new Location(current.row - 1, current.col))) {
List<Location> temp = new ArrayList<>(path);
temp.add(new Location(current.row - 1, current.col));
PathFind(temp);
}
}
} catch (Exception e) {
}
try {
if (map[current.row][current.col - 1] == 0) {
if (!path.contains(new Location(current.row, current.col - 1))) {
List<Location> temp = new ArrayList<>(path);
temp.add(new Location(current.row, current.col - 1));
PathFind(temp);
}
}
} catch (Exception e) {
}
try {
if (map[current.row + 1][current.col] == 0) {
if (!path.contains(new Location(current.row + 1, current.col))) {
List<Location> temp = new ArrayList<>(path);
temp.add(new Location(current.row + 1, current.col));
PathFind(temp);
}
}
} catch (Exception e) {
}
}
}

关于java - 0,1矩阵中的递归路径查找(并保存所有可能的路径)java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41451079/

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