-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2658.cpp
More file actions
38 lines (38 loc) · 1.21 KB
/
2658.cpp
File metadata and controls
38 lines (38 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int bfs(int i, int j, vector<vector<int>>& grid) {
int count = 0;
queue<pair<int, int>> q;
q.push({i, j});
count += grid[i][j];
grid[i][j] = 0;
int m = grid.size();
int n = grid[0].size();
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (auto& direction : directions) {
int nextX = x + direction.first;
int nextY = y + direction.second;
if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n) continue;
if (grid[nextX][nextY] == 0) continue;
q.push({nextX, nextY});
count += grid[nextX][nextY];
grid[nextX][nextY] = 0;
}
}
return count;
}
int findMaxFish(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
int count = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] > 0) count = max(count, bfs(i, j, grid));
}
}
return count;
}
};