0

A description to the problem: https://leetcode.com/problems/number-of-islands/

Basically you have a matrix with 1s and 0s, and you need to count how many groups of 1s there are.

Despite many print statements, I can't figure out why this code is not working. I'm iterating through the matrix and anytime I see a one, I do a depth first search and turn that 1 plus all the 1s surrounding it to 0 - to mark those nodes as visited.

What are I missing?

class Solution {
    public int numIslands(char[][] grid) {
        int count = 0;
        for(int i = 0; i < grid.length; i++) {
            for(int j = 0; j < grid[i].length; j++) {
                  //System.out.println(grid[i][j]);
                if(grid[i][j] == '1') {
                    // System.out.println("i = " + i);
                    // System.out.println("j = " + j);
                    countIslands(grid, i, j);
                    count++;
                }
            }
        }
        return count;
    }
    
    public static void countIslands(char[][] grid, int sr, int sc) {
        grid[sr][sc] = '0';
        
        final int[][] SHIFTS = {
            {0,1}, //move right
            {1,0}, //move down
            {0,-1}, //move left
            {-1,0} //move up
        };
        
        for(int[] shift : SHIFTS) {
            sr = sr + shift[0];
            sc = sc + shift[1];
            
            if(moveValid(grid, sr, sc)) {
                countIslands(grid, sr, sc);
            }
        }
    }
    
    public static boolean moveValid(char[][] grid, int sr, int sc) {
        if(sr >= 0 && sr < grid.length && sc >= 0 && sc < grid[sr].length && grid[sr][sc] == '1') {
            return true;
        }
        return false;
    }
}
j.Doie
  • 91
  • 1
  • 6
  • Please make your code [mre]: post hard coded data and the expected result. Also see [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – c0der Jan 26 '21 at 05:56

2 Answers2

1

Very nice problem. In my opinion, the mistake is in these lines:

for(int[] shift : SHIFTS) {
    sr = sr + shift[0];
    sc = sc + shift[1];
    
    if(moveValid(grid, sr, sc)) {
        countIslands(grid, sr, sc);
    }
}

Here you keep incrementing sr and sc while you're going round clockwise. So your search will go deeper than you would expect at the first glance.

papalulu
  • 78
  • 6
1

There's a mistake in this part of the code-

for(int[] shift : SHIFTS) {
        sr = sr + shift[0];
        sc = sc + shift[1];
        
        if(moveValid(grid, sr, sc)) {
            countIslands(grid, sr, sc);
        }
    }

You are modifying sr, sc and re-using it to get the other adjacent fields. Hence suppose initially sr=1, sc =1. All the adjacent cells for this are (1,0),(2,1),(0,1) and (1,2).

But in the above code, the adjacent cells are are constructed in this way, first sr=1 sc=1 then it's sr=1 sc=2, so further on you are using (1,2) to get the adjacent cells which will also include (1,3) which is not an adjacent cell for (1,1).

Correct way to do it will be-

for(int[] shift : SHIFTS) {
        int nsr = sr + shift[0];
        int nsc = sc + shift[1];
        
        if(moveValid(grid, nsr, nsc)) {
            countIslands(grid, nsr, nsc);
        }
    }
maddy
  • 114
  • 7