Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 34e9f2e

Browse files
committed
Add C++ solutions for leetcode 695.
1 parent 5798d7b commit 34e9f2e

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include<vector>
2+
#include<algorithm>
3+
#include<iostream>
4+
using namespace std;
5+
6+
class Solution {
7+
int dx[4] = {-1, 0, 1, 0};
8+
int dy[4] = {0, 1, 0, -1};
9+
int m, n;
10+
public:
11+
int maxAreaOfIsland(vector<vector<int>>& grid) {
12+
m = grid.size();
13+
n = grid[0].size();
14+
int res = 0;
15+
for (int i = 0; i < m; i++)
16+
{
17+
for (int j = 0; j < n; j++)
18+
{
19+
if (grid[i][j] == 1)
20+
res = max(res, dfs(grid, i, j));
21+
}
22+
}
23+
return res;
24+
}
25+
int dfs(vector<vector<int>>& grid, int x, int y) /* G[i][j] == 1时才调用dfs */
26+
{
27+
int res = 1; // 把当前格子加进来
28+
grid[x][y] = 0; /* 加完之后就将用水(0)代替之前的陆地(1), 目的是避免重复访问。用这种方法就不需要使用visited数组记录状态了 */
29+
for (int i = 0; i < 4; i++)
30+
{
31+
auto a = x + dx[i];
32+
auto b = y + dy[i];
33+
if (a >= 0 && a < m && b >= 0 && b < n) // 确保没越界
34+
{
35+
if (grid[a][b] == 1)
36+
res += dfs(grid, a, b); // 把连通块数累加一下
37+
}
38+
}
39+
return res;
40+
}
41+
};
42+
43+
// Test
44+
int main()
45+
{
46+
Solution sol;
47+
vector<vector<int>> grid =
48+
{
49+
{0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
50+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
51+
{0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
52+
{0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0},
53+
{0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0},
54+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
55+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
56+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}
57+
};
58+
auto res = sol.maxAreaOfIsland(grid);
59+
cout << res << endl;
60+
61+
return 0;
62+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include<vector>
2+
#include<algorithm>
3+
#include<iostream>
4+
using namespace std;
5+
6+
class Solution {
7+
int dx[4] = {-1, 0, 1, 0};
8+
int dy[4] = {0, 1, 0, -1};
9+
int m, n;
10+
public:
11+
int maxAreaOfIsland(vector<vector<int>>& grid) {
12+
m = grid.size();
13+
n = grid[0].size();
14+
int res = 0;
15+
/* 由于1 <= m, n <= 50, 故不需要判空 */
16+
int maxArea = 0;
17+
for (int i = 0; i < m; i++)
18+
{
19+
for (int j = 0; j < n; j++)
20+
maxArea = max(maxArea, dfs(grid, i, j));
21+
}
22+
return maxArea;
23+
}
24+
int dfs(vector<vector<int>>& grid, int x, int y)
25+
{
26+
// 判断当前传入坐标位置是否越界或是否为水域
27+
if(x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == 0)
28+
return 0;
29+
30+
grid[x][y] = 0; // 已访问过的岛屿也修改为0,避免重复访问
31+
int area = 1; // 每个岛屿的面积为1
32+
for (int i = 0; i < 4; i++)
33+
{
34+
auto a = x + dx[i];
35+
auto b = y + dy[i];
36+
area += dfs(grid, a, b);
37+
}
38+
return area;
39+
}
40+
};
41+
42+
// Test
43+
int main()
44+
{
45+
Solution sol;
46+
vector<vector<int>> grid =
47+
{
48+
{0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
49+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
50+
{0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
51+
{0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0},
52+
{0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0},
53+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
54+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
55+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}
56+
};
57+
auto res = sol.maxAreaOfIsland(grid);
58+
cout << res << endl;
59+
60+
return 0;
61+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /