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

时间:2017-01-03 19:37:46

标签: java recursion matrix

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

位置是一个具有行和列值(x,y)的对象。 locationEquals是一个函数,如果两个Locations相同则返回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'为空,这意味着它找不到方法。但是在这个矩阵中肯定有一种方法,所以这是我的代码中的一个我找不到的错误。

1 个答案:

答案 0 :(得分:1)

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

要解决此问题,我将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) {
        }
    }
}