-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber_of_distinct_elements.cpp
More file actions
75 lines (67 loc) · 2 KB
/
Number_of_distinct_elements.cpp
File metadata and controls
75 lines (67 loc) · 2 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
//{ Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution {
public:
void dfs(int row, int col, int Baserow0, int Basecol0,
vector<pair<int, int>> &vec, vector<vector<int>> &vis,
vector<vector<int>>& grid)
{
int n = grid.size();
int m = grid[0].size();
vis[row][col] = 1;
int delRow[] = {-1, 0, 1, 0};
int delCol[] = {0, +1, 0, -1};
vec.push_back({row - Baserow0, col - Basecol0});
for(int i = 0; i < 4; i++)
{
int nrow = row + delRow[i];
int ncol = col + delCol[i];
if(nrow >= 0 && nrow < n && ncol >= 0 && ncol < m
&& !vis[nrow][ncol] && grid[nrow][ncol] == 1)
{
dfs(nrow, ncol, Baserow0, Basecol0, vec, vis, grid);
}
}
}
int countDistinctIslands(vector<vector<int>>& grid) {
int n = grid.size();
int m = grid[0].size();
vector<vector<int>> vis(n, vector<int>(m, 0));
set<vector<pair<int, int>>> st;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(!vis[i][j] && grid[i][j] == 1)
{
vector<pair<int, int>> vec;
dfs(i, j, i, j, vec, vis, grid);
st.insert(vec);
}
}
}
return st.size();
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
}
}
Solution obj;
cout << obj.countDistinctIslands(grid) << endl;
}
}
// } Driver Code Ends