-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIslands2.java
More file actions
105 lines (82 loc) · 2.85 KB
/
Islands2.java
File metadata and controls
105 lines (82 loc) · 2.85 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
package PracticeGraphs;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Islands2 {
public static int findParent(int node, int parent[]) {
if (node == parent[node]) {
return node;
}
return parent[node] = findParent(parent[node], parent);
}
public static void union(int u, int v, int parent[]) {
int ulp = findParent(u, parent); // Find parent of u
int vlp = findParent(v, parent); // Find parent of v
if (ulp != vlp) {
parent[ulp] = vlp; // Union
}
}
public static List<Integer> fun(int[][] arr, int[][] arr2) {
int n = arr.length;
int m = arr[0].length;
int parent[] = new int[n * m];
Arrays.fill(parent, -1);
int components = 0;
List<Integer> islands = new ArrayList<>();
// Directions for exploring neighbors (up, right, down, left)
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
for (int[] it : arr2) {
int x = it[0];
int y = it[1];
int cell = x * m + y;
// If already land, return current number of components
if (parent[cell] != -1) {
islands.add(components);
continue;
}
// Add new land
parent[cell] = cell;
components++;
// Check neighbors
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
int neighbor = nx * m + ny;
if (parent[neighbor] != -1) { // Neighbor is part of the grid
int parentCell = findParent(cell, parent);
int parentNeighbor = findParent(neighbor, parent);
if (parentCell != parentNeighbor) {
union(cell, neighbor, parent);
components--; // Merge reduces the number of components
}
}
}
}
islands.add(components);
}
return islands;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int arr[][] = new int[n][m]; // Initial grid
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = sc.nextInt();
}
}
int n2 = sc.nextInt();
int arr2[][] = new int[n2][2];
for (int i = 0; i < n2; i++) {
for (int j = 0; j < 2; j++) {
arr2[i][j] = sc.nextInt();
}
}
System.out.println(fun(arr, arr2));
sc.close();
}
}