forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-distinct-islands.cpp
39 lines (35 loc) · 1.13 KB
/
number-of-distinct-islands.cpp
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
// Time: O(m * n)
// Space: O(m * n)
class Solution {
public:
int numDistinctIslands(vector<vector<int>>& grid) {
unordered_set<string> islands;
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[0].size(); ++j) {
string island;
if (dfs(i, j, &grid, &island)) {
islands.emplace(island);
}
}
}
return islands.size();
}
private:
bool dfs(const int i, const int j,
vector<vector<int>> *grid, string *island) {
static const unordered_map<char, pair<int, int>>
directions = { {'l', {-1, 0} }, {'r', { 1, 0} },
{'u', { 0, 1} }, {'d', { 0, -1} }};
if (i < 0 || i >= grid->size() ||
j < 0 || j >= (*grid)[0].size() ||
(*grid)[i][j] <= 0) {
return false;
}
(*grid)[i][j] *= -1;
for (const auto& kvp : directions) {
island->push_back(kvp.first);
dfs(i + kvp.second.first, j + kvp.second.second, grid, island);
}
return true;
}
};