forked from sourav-122/hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number of Distinct Islands.cpp
42 lines (38 loc) · 1.23 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
40
41
42
class Solution {
public:
void dfs(int r,int c,int m,int n,vector<vector<int>>&vis,vector<vector<int>>&grid,vector<pair<int,int>>&v){
if(r<0 || c<0 || r>=m ||c>=n || vis[r][c] || grid[r][c]==0){
return;
}
v.push_back({r,c});
vis[r][c]=1;
dfs(r+1,c,m,n,vis,grid,v);
dfs(r-1,c,m,n,vis,grid,v);
dfs(r,c+1,m,n,vis,grid,v);
dfs(r,c-1,m,n,vis,grid,v);
}
int countDistinctIslands(vector<vector<int>>& grid) {
int count=0;
int m=grid.size();
int n=grid[0].size();
vector<vector<int>>vis(m,vector<int>(n,0));
set<vector<pair<int,int>>>s;
for(int r=0;r<m;r++){
for(int c=0;c<n;c++){
if(!vis[r][c] && grid[r][c]==1){
vector<pair<int,int>>v;
dfs(r,c,m,n,vis,grid,v);
auto t=v[0];
for(auto &it:v){
it.first-=t.first;
it.second-=t.second;
}
if(s.find(v)==s.end()){
s.insert(v);
}
}
}
}
return s.size();
}
};