LeetCode Number of Islands 岛的数量(DFS,BFS)

题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛?

思路:DFS还是比较短,实现了一下。如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的。

 1 class Solution {
 2 public:
 3     bool isok(vector<vector<char> >& grid,int x,int y)
 4     {
 5         return x>=0 && y>=0 && x<grid.size() && y<grid[0].size();
 6     }
 7
 8     void DFS( vector<vector<char> >& grid,int x,int y )
 9     {
10         if( !isok(grid,x,y) || grid[x][y]==‘0‘  )   return ;
11
12         grid[x][y]=‘0‘;
13         DFS( grid, x, y+1 );
14         DFS( grid, x, y-1 );
15         DFS( grid, x+1, y );
16         DFS( grid, x-1, y );
17     }
18
19     int numIslands(vector<vector<char> >& grid) {
20         int ans=0;
21         for(int i=0; i<grid.size(); i++)
22         {
23             for(int j=0; j<grid[0].size(); j++)
24             {
25                 if(grid[i][j]==‘1‘)
26                 {
27                     DFS(grid, i, j);
28                     ans++;
29                 }
30             }
31         }
32         return ans;
33     }
34 };

AC代码

时间: 2024-10-11 05:22:53

LeetCode Number of Islands 岛的数量(DFS,BFS)的相关文章

[LeetCode] 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

[LeetCode] Number of Islands II 岛屿的数量之二

A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand o

第八篇 LeetCode Number of Islands

好久没更新了,终于考完了继续回来刷题 岛屿问题属于最基本的DFS, BFS题目 使用DFS时会遇到如果图太大call stack过深的follow up,此时可以转为使用BFS 下一篇Number of Islands II 中将使用另一种Union Find的做法 public class Solution { public int numIslands(char[][] grid) { if (grid == null || grid.length == 0) { return 0; } i

[LintCode] Number of Islands 岛屿的数量

Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is represented as the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent. Have you met this ques

[LeetCode] Number of Islands

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

[LeetCode] Number of Islands II

Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands

第九篇 LeetCode Number of Islands II

UnifonFind with path compression and weighting O(klogmn), k is the length of positions array public class Solution { public List<Integer> numIslands2(int m, int n, int[][] positions) { List<Integer> result = new ArrayList<Integer>(); if

leetcode 200. 岛屿数量(dfs||bfs)

给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入:11110110101100000000 输出: 1示例 2: 输入:11000110000010000011 输出: 3 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/number-of-islands class Solution { pu

[LeetCode] 695. Max Area of Island_Easy tag: DFS/BFS

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Find the maximum area of an island