[LintCode] Number of Islands(島嶼個數)

描述

給一個01矩陣,求不一樣的島嶼的個數。java

0表明海,1表明島,若是兩個1相鄰,那麼這兩個1屬於同一個島。咱們只考慮上下左右爲相鄰。git

樣例

在矩陣:github

[
  [1, 1, 0, 0, 0],
  [0, 1, 0, 0, 1],
  [0, 0, 0, 1, 1],
  [0, 0, 0, 0, 0],
  [0, 0, 0, 0, 1]
]

中有 3 個島。數組

 

代碼

GitHub 的源代碼,請訪問下面的連接:spa

https://github.com/cwiki-us/java-tutorial/blob/master/src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode0433NumIslandsTest.javadebug

package com.ossez.lang.tutorial.tests.lintcode;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * 433
 * <ul>
 * <li>@see <a href=
 * "https://www.cwiki.us/display/ITCLASSIFICATION/Number+of+Islands">https://www.cwiki.us/display/ITCLASSIFICATION/Number+of+Islands</a>
 * <li>@see<a href="https://www.lintcode.com/problem/number-of-islands/">https://www.lintcode.com/problem/number-of-islands/</a>
 * </ul>
 * </p>
 * 
 * @author YuCheng
 *
 */
public class LintCode0433NumIslandsTest {

  private final static Logger logger = LoggerFactory.getLogger(LintCode0433NumIslandsTest.class);

  /**
   * 
   */
  @Test
  public void testMain() {
    logger.debug("BEGIN");
    // INIT GRID
    boolean[][] grid = { { true, true, false, false, false }, { false, true, false, false, true }, { false, false, false, true, true },
        { false, false, false, false, false }, { false, false, false, false, true }

    };

    // NULL CHECK
    if (grid.length == 0 || grid[0].length == 0) {
      System.out.println("NULL");
      // return 0;
    }

    // GET SIZE
    int n = grid.length;
    int m = grid[0].length;

    // ARRAY FOR VISITED LOG
    boolean[][] visited = new boolean[n][m];

    int count = 0;

    // LOOP FOR GRID
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (grid[i][j] && !visited[i][j]) {
          numIslandsDFS(grid, visited, i, j);
          count++;
        }
      }
    }

    System.out.println(count);

  }

  /**
   * 
   * @param grid
   * @param visited
   * @param x
   * @param y
   */
  public void numIslandsDFS(boolean[][] grid, boolean[][] visited, int x, int y) {
    if (x < 0 || x >= grid.length) {
      return;
    }

    if (y < 0 || y >= grid[0].length) {
      return;
    }

    if (grid[x][y] != true || visited[x][y]) {
      return;
    }

    visited[x][y] = true;

    // Recursive call
    numIslandsDFS(grid, visited, x - 1, y);
    numIslandsDFS(grid, visited, x + 1, y);
    numIslandsDFS(grid, visited, x, y - 1);
    numIslandsDFS(grid, visited, x, y + 1);

  }
}

 

點評

本質是求矩陣中連續區域的個數,很容易想到須要用深度優先搜索 DFS 來解,咱們須要創建一個 visited 數組用來記錄某個位置是否被訪問過,對於一個爲 true 且未被訪問過的位置,咱們遞歸進入其上下左右位置上爲 true 的數,將其 visited 對應值賦爲 true,繼續進入其全部相連的鄰位置,這樣能夠將這個連通區域全部的數找出來,並將其對應的 visited 中的值賦 true,找完次區域後,咱們將結果 res 自增 1,而後咱們在繼續找下一個爲 true 且未被訪問過的位置,以此類推直至遍歷完整個原數組便可獲得最終結果。code

這裏須要有一個遞歸的調用。在遞歸調用以前須要進行判斷是否超出邊際,若是超出邊際的話,就要跳出循環。遞歸

在一個節點進行遍歷的時候,須要在遞歸調用的時候,同時針對這個節點搜索上下左右 4 個節點,若是找到須要了知足條件的 true,就繼續查找,若是沒有找到就退出。在這個過程的時候,須要將訪問過的節點保存到訪問控制的 2 維數組中。以便於在下次查找的時候跳過這個節點。get

https://www.cwiki.us/display/ITCLASSIFICATION/Number+of+Islandsit

相關文章
相關標籤/搜索