forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
33 lines (32 loc) · 997 Bytes
/
Solution.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
class Solution {
public:
int numDistinctIslands(vector<vector<int>>& grid) {
unordered_set<string> paths;
string path;
for (int i = 0; i < grid.size(); ++i)
{
for (int j = 0; j < grid[0].size(); ++j)
{
if (grid[i][j] == 1)
{
path = "";
dfs(i, j, 0, grid, path);
paths.insert(path);
}
}
}
return paths.size();
}
void dfs(int i, int j, int direction, vector<vector<int>>& grid, string& path) {
grid[i][j] = 0;
path += to_string(direction);
vector<int> dirs = {-1, 0, 1, 0, -1};
for (int k = 1; k < 5; ++k)
{
int x = i + dirs[k - 1], y = j + dirs[k];
if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && grid[x][y] == 1)
dfs(x, y, k, grid, path);
}
path += to_string(direction);
}
};