200. Number of Islands && 130. Surrounded Regions

200. Number of Islands

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

11110
11010
11000
00000

Answer: 1

Example 2:

11000
11000
00100
00011

Answer: 3

 
 
public class Solution {
    int R = -1;
    int C = -1;
    public int numIslands(char[][] grid) {
        R = grid.length;
        if(R == 0)
            return 0;
            
        C = grid[0].length;
        
        int count = 0;
        for(int r = 0; r<R; ++r)
        {
            for(int c = 0; c<C; ++c)
            {
                if(grid[r][c] == '1')
                {
                    ++count;
                }
                changeIslandsToWater(grid, r, c);
            }
            
        }
        return count;
    }
    
    private void changeIslandsToWater(char[][] grid, int r, int c)
    {
        if(r>=R || c >=C || r<0 || c<0)
            return;
        if(grid[r][c] == '0')
            return;
        
        grid[r][c] = '0';
        changeIslandsToWater(grid, r, c+1); //go right
        changeIslandsToWater(grid, r+1, c); //go down
        changeIslandsToWater(grid, r, c-1); //go left
        changeIslandsToWater(grid, r-1, c); //go up
        
    }
}

 

130. Surrounded Regions

Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.

A region is captured by flipping all 'O's into 'X's in that surrounded region.

For example,

X X X X
X O O X
X X O X
X O X X

 

After running your function, the board should be:

X X X X
X X X X
X X X X
X O X X

 

Hide Similar Problems
 (M) Number of Islands (M) Walls and Gates
 
public class Solution {
  public void solve(char[][] board) {
    if (board == null)
      return;
    int rows = board.length;
    if (rows <= 2)
      return;
    int columns = board[0].length;
    if (columns <= 2)
      return;

    //First change all Os that connect to boundaries to some temporary characters
    for (int c = 0; c < columns; ++c) { //change first and last row
      if (board[0][c] == 'O')
        changeBoard(board, 0, c);
      if (board[rows - 1][c] == 'O')
        changeBoard(board, rows - 1, c);
    }
    for (int r = 1; r < rows - 1; ++r) { //change first and last column
      if (board[r][0] == 'O')
        changeBoard(board, r, 0);
      if (board[r][columns - 1] == 'O')
        changeBoard(board, r, columns - 1);
    }
    
    //Then change all Os to Xs, change all temporary characters back to Os.
    for (int r = 0; r < rows; ++r)
      for (int c = 0; c < columns; ++c) {
        if (board[r][c] == 'O')
          board[r][c] = 'X';
        if (board[r][c] == 'T')
          board[r][c] = 'O';
      }
  }

  private void changeBoard(char[][] board, int r, int c) {
    if (board[r][c] == 'O')
      board[r][c] = 'T';

    if (c < board[0].length - 1 && board[r][c + 1] == 'O')
      changeBoard(board, r, c + 1); //go right
    if (r < board.length - 1 && board[r + 1][c] == 'O')
      changeBoard(board, r + 1, c); //go down
    if (c > 1 && board[r][c - 1] == 'O')
      changeBoard(board, r, c - 1); //go left. Note that it is very sensitive here, if you set c>=1, 
// you may get a stack overflow exception because you are converting connected Os to Ts, which could be better solved by splitting
// the work into two stages.
if (r > 1 && board[r - 1][c] == 'O') changeBoard(board, r - 1, c); //go up. Sensitive here as well, if setting r>=1. } }

 

 
 
 
posted @ 2016-06-08 12:08  新一代的天皇巨星  阅读(170)  评论(0)    收藏  举报