-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_434_NumberOfIslandsII.cpp
More file actions
106 lines (83 loc) · 2.51 KB
/
Copy path_434_NumberOfIslandsII.cpp
File metadata and controls
106 lines (83 loc) · 2.51 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Source - https://www.lintcode.com/problem/number-of-islands-ii/description/
Author - Shivam Arora
*/
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y;
Point(int a, int b) {
x = a;
y = b;
}
};
int dsFind(int i, vector<int>& parent) {
if(parent[i] == i)
return i;
else {
int result = dsFind(parent[i], parent);
parent[i] = result;
return result;
}
}
vector<pair<int, int>> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void dsUnion(vector<vector<int>>& grid, vector<int>& parent, vector<int>& rank, int x, int y, int& count) {
grid[x][y] = 1;
int w = grid[0].size();
for(int i = 0; i < dirs.size(); i++) {
int curr = dsFind((x * w) + y, parent);
int a = x + dirs[i].first, b = y + dirs[i].second;
if(a >= 0 && a < grid.size() && b >= 0 && b < grid[0].size() && grid[a][b] == 1) {
int adj = dsFind((a * w) + b, parent);
if(curr != adj) {
if(rank[curr] > rank[adj])
parent[adj] = curr;
else if(rank[curr] < rank[adj])
parent[curr] = adj;
else {
parent[curr] = adj;
rank[adj]++;
}
count--;
}
}
}
}
vector<int> numIslands2(int n, int m, vector<Point> &operators) {
vector<vector<int>> grid(n, vector<int> (m));
int size = n * m;
vector<int> parent(size, -1);
vector<int> rank(size, 1);
vector<int> result;
int count = 0;
for(int i = 0; i < operators.size(); i++) {
int idx = (operators[i].x * m) + operators[i].y;
if(parent[idx] == -1) {
parent[idx] = idx;
count++;
dsUnion(grid, parent, rank, operators[i].x, operators[i].y, count);
}
result.push_back(count);
}
return result;
}
int main()
{
int n, m;
cout<<"Enter grid dimensions: ";
cin>>n>>m;
int k;
cout<<"Enter number of operators: ";
cin>>k;
vector<Point> operators;
int x, y;
cout<<"Enter operators (x-coordinate y-coordinate): "<<endl;
for(int i = 0; i < k; i++) {
cin>>x>>y;
operators.push_back(Point(x, y));
}
vector<int> result = numIslands2(n, m, operators);
cout<<"Output: [";
for(int i = 0; i < result.size() - 1; i++)
cout<<result[i]<<", ";
cout<<result[result.size() - 1]<<"]"<<endl;
}