LeetCode - Number of Islands

Number of Islands

2015.4.17 06:16

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

Solution1:

  Solutiuon using DFS.

Accepted code:

 1 // 3CE, 1WA, 1AC, solution using DFS
 2 class Solution {
 3 public:
 4     Solution() {
 5         d[0][0] = -1;
 6         d[0][1] = 0;
 7         d[1][0] = +1;
 8         d[1][1] = 0;
 9         d[2][0] = 0;
10         d[2][1] = -1;
11         d[3][0] = 0;
12         d[3][1] = +1;
13     }
14 
15     int numIslands(vector<vector<char>> &grid) {
16         n = grid.size();
17         if (n == 0) {
18             return 0;
19         }
20         m = grid[0].size();
21         if (m == 0) {
22             return 0;
23         }
24         
25         int cc = 0;
26         int i, j;
27         
28         b.resize(n, vector<bool>(m, false));
29         for (i = 0; i < n; ++i) {
30             for (j = 0; j < m; ++j) {
31                 if (grid[i][j] == '1' && !b[i][j]) {
32                     DFS(i, j, grid);
33                     ++cc;
34                 }
35             }
36         }
37         b.clear();
38         
39         return cc;
40     }
41 protected:
42     int n, m;
43     vector<vector<bool> > b;
44     int d[4][2];
45     
46     void DFS(int x, int y, vector<vector<char> > &grid) {
47         b[x][y] = true;
48         int i;
49         int x1, y1;
50         
51         for (i = 0; i < 4; ++i) {
52             x1 = x + d[i][0];
53             y1 = y + d[i][1];
54             if (x1 < 0 || x1 > n - 1 || y1 < 0 || y1 > m - 1) {
55                 continue;
56             }
57             if (grid[x1][y1] == '0' || b[x1][y1]) {
58                 continue;
59             }
60             DFS(x1, y1, grid);
61         }
62     }
63 };

Solutiuon2:

  Solution using BFS.

Accepted code:

 1 // 1AC, solution using BFS
 2 #include <queue>
 3 using namespace std;
 4 
 5 class Solution {
 6 public:
 7     Solution() {
 8         d[0][0] = -1;
 9         d[0][1] = 0;
10         d[1][0] = +1;
11         d[1][1] = 0;
12         d[2][0] = 0;
13         d[2][1] = -1;
14         d[3][0] = 0;
15         d[3][1] = +1;
16     }
17 
18     int numIslands(vector<vector<char>> &grid) {
19         n = grid.size();
20         if (n == 0) {
21             return 0;
22         }
23         m = grid[0].size();
24         if (m == 0) {
25             return 0;
26         }
27         
28         int cc = 0;
29         int i, j;
30         
31         b.resize(n, vector<bool>(m, false));
32         for (i = 0; i < n; ++i) {
33             for (j = 0; j < m; ++j) {
34                 if (grid[i][j] == '1' && !b[i][j]) {
35                     BFS(i, j, grid);
36                     ++cc;
37                 }
38             }
39         }
40         b.clear();
41         
42         return cc;
43     }
44 protected:
45     int n, m;
46     vector<vector<bool> > b;
47     int d[4][2];
48     
49     void BFS(int x, int y, vector<vector<char> > &grid) {
50         queue<int> q;
51         int i;
52         int x1, y1;
53         int p;
54         
55         q.push(x * m + y);
56         b[x][y] = true;
57         while (!q.empty()) {
58             p = q.front();
59             q.pop();
60             x = p / m;
61             y = p % m;
62             for (i = 0; i < 4; ++i) {
63                 x1 = x + d[i][0];
64                 y1 = y + d[i][1];
65                 if (x1 < 0 || x1 > n - 1 || y1 < 0 || y1 > m - 1) {
66                     continue;
67                 }
68                 if (grid[x1][y1] == '0' || b[x1][y1]) {
69                     continue;
70                 }
71                 q.push(x1 * m + y1);
72                 b[x1][y1] = true;
73             }
74         }
75     }
76 };

 

转载于:https://www.cnblogs.com/zhuli19901106/p/4433864.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值