简体   繁体   English

无限递归帮助-Java

[英]Infinite Recursion Help - Java

OK, I have a small error - it seems that I am getting an infinite recursion on my program that I am trying to solve. 好的,我有一个小错误-似乎我要解决的程序正在无限递归。 So basically, there is an array that you read in, and then taking those values, randomly load them into a 2d array. 因此,基本上,您要读取一个数组,然后获取这些值,然后将它们随机加载到2d数组中。 I have all the set up and other methods done for the program, but my recursion methods seem to be going into infinite recursion. 我已经为程序完成了所有设置并完成了其他方法,但是我的递归方法似乎正在进入无限递归。 My compiler won't tell me what is wrong with it and i can't see the actual output except for the errors on the recursion statements. 我的编译器不会告诉我它有什么问题,除了递归语句中的错误外,我看不到实际的输出。 I have done similar problems like this before, and this is the first time this error has happened, can someone point me in the right direction? 我以前也做过类似的问题,这是第一次发生此错误,有人可以向我指出正确的方向吗?

public class Grid
{
    private String[][] grid;
    private int tempCount = 0;

    public Grid()
    {
        grid = new String[10][10];
    }
    public Grid(int rows, int cols, String[] vals)
    {
        setGrid(rows, cols, vals);
    }
    public void setGrid(int rows, int cols, String[] vals)
    {
        grid = new String[rows][cols];
        for(int r = 0; r < grid.length; r++)
            for(int c = 0; c < grid[r].length; c++)
                grid[r][c] = vals[(int)(Math.random() * vals.length)];
    }
    public int findMax(String val)
    {
        int max = 0;
        for(int r = 0; r < grid.length; r++)
        {
            for(int c = 0; c < grid[r].length; c++)
            {
                if(grid[r][c].equals(val))
                {
                    tempCount = 0;
                    int temp = findMaxHelper(r, c, val);
                    if(max < temp)
                        max = temp;
                }
            }
        }
        return max;
    }
    private int findMaxHelper(int r, int c, String search)
    {
        if(r < grid.length && r >= 0 && c < grid[r].length && c >= 0 && grid[r][c].equals(search))
        {
            tempCount++;
            findMaxHelper(r - 1, c, search);
            findMaxHelper(r + 1, c, search);
            findMaxHelper(r, c - 1, search);
            findMaxHelper(r, c + 1, search);
        }
        return tempCount;
    }
    public String toString()
    {
        String output = "";
        for(int r = 0; r < grid.length; r++)
        {
            for(int c = 0; c < grid[r].length; c++)
                output += grid[r][c] + " ";
            output += "\n";
        }
        return output;
    }
}

The problem is this: assume we have a simple array with the dimension 2x1, and the values ["5", "5"]. 问题是这样的:假设我们有一个简单的数组,其维度为2x1,其值为[“ 5”,“ 5”]。 Suggest we're looking for "5". 建议我们在寻找“ 5”。 Then it will hit the first five. 然后它将达到前五个。 From there on it will check all its neighbors. 从那里开始,它将检查所有邻居。 So it will hit the second five, from where it will search all neighbors again. 因此它将命中第二个五个,从那里将再次搜索所有邻居。 So it will hit the first five, from where it will search all neighbors again. 因此它将命中前五个,从那里将再次搜索所有邻居。 So it will hit the second five, from where it will search all neighbors again. 因此它将命中第二个五个,从那里将再次搜索所有邻居。 So it will hit the first five, from where it will search all neighbors again. 因此它将命中前五个,从那里将再次搜索所有邻居。 So it will hit the second five, from where it will search all neighbors again. 因此它将命中第二个五个,从那里将再次搜索所有邻居。 So it will hit the first five, from where it will search all neighbors again. 因此它将命中前五个,从那里将再次搜索所有邻居。

... you get the pattern^^ ...你会得到模式^^

You need to add an extra parameter to the findMaxHelper method to record the positions you have already visited. 您需要在findMaxHelper方法中添加一个额外的参数,以记录您已经访问过的位置。 The reason for this is well-explained in the answer of @JayC667. @ JayC667的答案对此作了很好的解释。

private int findMaxHelper(int r, int c, String search, boolean[][] dejaVu)
{
    if(r < grid.length && r >= 0 && c < grid[r].length && c >= 0 && !dejaVu[r][c] && grid[r][c].equals(search)) 
    {
        tempCount++;
        dejaVu[r][c] = true;
        findMaxHelper(r - 1, c, search, dejaVu);
        findMaxHelper(r + 1, c, search, dejaVu);
        findMaxHelper(r, c - 1, search, dejaVu);
        findMaxHelper(r, c + 1, search, dejaVu);
    }
    return tempCount;
}

then you need to pass new boolean[10][10] as dejaVu every time you begin a new search. 那么您每次开始新搜索时都需要将new boolean[10][10]作为dejaVu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM