Advertisement

LeetCode(200)-问题:寻找陆地(深度优先搜索算法)

阅读量:
题目:

Given a 2D grid map consisting of '1's (representing land) and '0's (representing water), calculate the total number of islands. An island is defined as a contiguous group of land cells that are connected either horizontally or vertically and is enclosed by water on all sides. It is assumed that all four edges of the grid are surrounded by water.

Example 1:

Input:
11110
11010
11000
00000

Output: 1
Example 2:

Input:
11000
11000
00100
00011

Output: 3

翻译:

给定一个二进制矩阵(其中"1"表示陆地,"0"表示水域),请统计由1构成的连通区域的数量。每个由1构成的区域会被0包围,并且相邻格子中如果有相同的1则被视为同一岛屿。你可以假设该矩阵四周被水域完全包围着

全部评论 (0)

还没有任何评论哟~