-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisland-perimeter.cpp
More file actions
84 lines (72 loc) · 1.98 KB
/
island-perimeter.cpp
File metadata and controls
84 lines (72 loc) · 1.98 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 463. Island Perimeter (11/25/57673)
// Runtime: 49 ms (1.16%) Memory: 118.23 MB (0.00%)
/*
* Time complexity O(n * m)
* Space complexity O(n * m)
*/
class Solution {
struct Cell
{
int x;
int y;
bool operator<(const Cell& other) const
{
if(x != other.x) return x < other.x;
return y < other.y;
}
};
public:
int islandPerimeter(vector<vector<int>>& grid) {
ROWS = grid.size();
COLS = grid[0].size();
for(int i=0; i < ROWS; i++)
{
for(int j=0; j < COLS; j++)
{
if(grid[i][j] == 1)
{
return bfs(grid, {i, j});
}
}
}
return 0;
}
int bfs(const auto& grid, const Cell& start)
{
frontier.push(start);
processed.insert(start);
int perimeter = 0;
while(! frontier.empty())
{
const auto curr = frontier.front(); frontier.pop();
for(const auto& [dx, dy] : DIRS)
{
const Cell next{curr.x + dx, curr.y + dy};
if(next.x >=0 && next.x < ROWS
&& next.y >=0 && next.y < COLS
&& processed.find(next) == processed.end()
&& grid[next.x][next.y] == 1)
{
frontier.push(next);
processed.insert(next);
}
else if(next.x < 0 || next.x >= ROWS
|| next.y < 0 || next.y >= COLS
|| grid[next.x][next.y] == 0)
{
// Everything else contributes to perimeter:
// - Out of bounds
// - Water cells (grid[x][y] == 0)
perimeter++;
}
}
}
return perimeter;
}
private:
int ROWS;
int COLS;
std::vector<Cell> DIRS = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
std::queue<Cell> frontier;
std::set<Cell> processed;
};