-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek2_Day4.java
More file actions
89 lines (79 loc) · 2.73 KB
/
Week2_Day4.java
File metadata and controls
89 lines (79 loc) · 2.73 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
package Algorithm2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
public class Week2_Day4 {
public static Set<Baechoo> visit;
public static Set<Baechoo> location;
public static int[] dx = {0, 0, 1, -1};
public static int[] dy = {1, -1, 0, 0};
public static int M;
public static int N;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int j = 0; j < T; j++) {
String[] info = br.readLine().split(" ");
M = Integer.parseInt(info[0]);
N = Integer.parseInt(info[1]);
int K = Integer.parseInt(info[2]);
location = new HashSet<>();
visit = new HashSet<>();
for (int i = 0; i < K; i++) {
String[] each = br.readLine().split(" ");
int each_x = Integer.parseInt(each[0]);
int each_y = Integer.parseInt(each[1]);
Baechoo new_b = new Baechoo(each_x, each_y);
location.add(new_b);
}
int count = 0;
for (int i = 0; i < M; i++) {
for (int k = 0; k < N; k++) {
Baechoo new_bb = new Baechoo(i, k);
if (location.contains(new_bb) && !visit.contains(new_bb)) {
dfs(new_bb);
count++;
}
}
}
System.out.println(count);
}
}
private static void dfs(Baechoo bc) {
visit.add(bc);
for (int j = 0; j < 4; j++) {
int new_x = bc.x + dx[j];
int new_y = bc.y + dy[j];
if (0 <= new_x && new_x < M && 0 <= new_y && new_y < N) { // 경계 검사 수정
Baechoo new_b = new Baechoo(new_x, new_y);
if (location.contains(new_b) && !visit.contains(new_b)) {
dfs(new_b);
}
}
}
}
}
class Baechoo {
int x;
int y;
public Baechoo(int x, int y) {
this.x = x;
this.y = y;
}
///////////////?????????? 이렇게 해야 같은 좌표의 객체를 동일하게처리??
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Baechoo baechoo = (Baechoo) o;
return x == baechoo.x && y == baechoo.y;
}
@Override
public int hashCode() {
return 31 * x + y;
}
}