-
Notifications
You must be signed in to change notification settings - Fork 1
/
694.cpp
56 lines (47 loc) · 1.69 KB
/
694.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Number of Distinct Islands
// Typical DFS problem. The hard part is how to save the shape of the island
// One clever way from the internet is save all the offest (x, y) relative to
// the top left most cell inside the island.
#include <vector>
#include <set>
using namespace std;
class Solution {
public:
int numDistinctIslands(vector<vector<int> >& grid) {
if (grid.empty() || grid[0].empty())
return 0;
M = grid.size();
N = grid[0].size();
set<vector<vector<int> > > islands;
for (int r = 0; r < M; r++)
for (int c = 0; c < N; c++) {
if (grid[r][c] > 0) {
vector<vector<int> > island;
DFS(r, c, r, c, grid, island);
islands.insert(island);
}
}
return islands.size();
}
private:
int M; // grid rows
int N; // grid cols
void DFS(int sr, int sc, int r, int c, vector<vector<int> >& grid, vector<vector<int> >& island) {
if (r < 0 || r >= M || c < 0 || c >= N || grid[r][c] <= 0 /* visited or is water */)
return;
island.push_back({r - sr, c - sc});
grid[r][c] = -1; // mark as visited
DFS(sr, sc, r + 1, c, grid, island);
DFS(sr, sc, r - 1, c, grid, island);
DFS(sr, sc, r, c - 1, grid, island);
DFS(sr, sc, r, c + 1, grid, island);
}
};
int main(void) {
vector<vector<int> > grid = { {1, 1, 0, 1, 1},
{1, 0, 0, 0, 0},
{0, 0, 0, 0, 1},
{1, 1, 0, 1, 1}};
Solution sol;
return sol.numDistinctIslands(grid);
}