Praneet Paidisetty
Praneet Paidisetty

Reputation: 1

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. 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;
    }
}

Upvotes: 0

Views: 237

Answers (2)

Paul Boddington
Paul Boddington

Reputation: 37645

You need to add an extra parameter to the findMaxHelper method to record the positions you have already visited. The reason for this is well-explained in the answer of @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.

Upvotes: 1

JayC667
JayC667

Reputation: 2576

The problem is this: assume we have a simple array with the dimension 2x1, and the values ["5", "5"]. Suggest we're looking for "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^^

Upvotes: 2

Related Questions