原题地址
https://leetcode.com/problems/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.
给出一个2D的地图,由陆地(1)和水(0)组成,数出岛的个数.一个岛被水环绕并且与相邻的陆地连接.你可以假设方阵的四周被水环绕.
Example 1:
11110
11010
11000
00000
Answer: 1
Example 2:
11000
11000
00100
00011
Answer: 3
Tags Breadth-first Search
Depth-first Search
解题思路
采用广度优先遍历的方法还是很容易解决这个问题的,我们尝试遍历所有的点,如果一个点是陆地且从未遍历过,则认为发现了新岛屿,在发现了新岛屿后使用广度优先的方式扩展该岛屿以防止重复计数.