【问题标题】:Recursive Path Finding in 0,1 matrix (and saving all possible paths) java0,1矩阵中的递归路径查找(并保存所有可能的路径)java
【发布时间】:2017-01-03 19:37:45
【问题描述】:

我正在尝试编写一个递归方法,该方法将在不回溯到包含值 0,1 的 int 矩阵中的位置的情况下找到路径。 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'为空,表示没有找到方法。但是这个矩阵中肯定有办法,所以这是我的代码中的一个我找不到的错误。

【问题讨论】:

  • 这听起来是一个学习如何使用调试器的好机会。
  • 提示:List&lt;Location&gt; temp = path; 不会做你认为的那样。
  • 问题是我无法在我正在编码的环境中调试我的程序。它是某种 webkit,我必须使用它。虽然当我确实使用某种调试选项时,它表明“路径”大小以某种方式达到 50+,并且递归没有停止,并且它表明从 PathFind 的第一次迭代开始,它只去了一个选项4 个中的一个,显然我可以分支到所有 4 个。
  • debug help ... 请展示您调试此问题的尝试。您发布的代码甚至没有额外的 print*8 语句来跟踪中间变量的值。例如,按照**路径
  • @EliranZiv 要在List 上执行半深度复制,请创建新的temp 列表并将其实例化为ArrayList。然后使用temp.addAll(path); 添加来自path 的所有项目。这将使temp成为一个独立的女人错误List

标签: java recursion matrix


【解决方案1】:

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

为了解决这个问题,我将List&lt;Location&gt; temp = path; 替换为List&lt;Location&gt; temp = new ArrayList&lt;&gt;(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) {
        }
    }
}

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2017-05-06
    相关资源
    最近更新 更多