Hello everyone, can someone help me to solve this problem .
In my script i count number of cluster in this matrix, but i need to return the coordinates x,y of each cluster founded.
- #include <bits/stdc++.h>
- using namespace std;
-
-
- vector<vector<int> > dirs = { { 0, -1 },
- { -1, 0 },
- { 0, 1 },
- { 1, 0 } };
-
- void dfs(vector<vector<int> >& grid, int x0, int y0,
- int i, int j, vector<pair<int, int> >& v)
- {
- int rows = grid.size(), cols = grid[0].size();
- if (i < 0 || i >= rows || j < 0
- || j >= cols || grid[i][j] <= 0)
- return;
-
- grid[i][j] *= -1;
-
- v.push_back({ i - x0, j - y0 });
-
- for (auto dir : dirs) {
- dfs(grid, x0, y0, i + dir[0], j + dir[1], v);
- }
- }
-
-
- int countDistinctIslands(vector<vector<int> >& grid)
- {
- int rows = grid.size();
- if (rows == 0)
- return 0;
- int cols = grid[0].size();
- if (cols == 0)
- return 0;
- set<vector<pair<int, int> > > coordinates;
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j) {
-
-
- if (grid[i][j] != 1)
- continue;
-
-
- vector<pair<int, int> > v;
- dfs(grid, i, j, i, j, v);
-
-
- coordinates.insert(v);
- }
- }
- return coordinates.size();
- }
-
- int main()
- {
- vector<vector<int> > grid = {{0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1},
- {0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0},
- {1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0},
- {0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0},
- {0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0},
- {1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1},
- {0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0},
- {1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0},
- {0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1},
- {1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1}};
- cout << "Number of distinct islands is "
- << countDistinctIslands(grid);
- return 0;
- }