This repository was archived by the owner on Dec 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ4963.java
More file actions
72 lines (63 loc) · 2.59 KB
/
Copy pathBOJ4963.java
File metadata and controls
72 lines (63 loc) · 2.59 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class BOJ4963 {
static int w;
static int h;
static int map[][];
static boolean visted[][];
static int answer;
static int xDir[] = new int[]{-1, -1, -1, 0, 1, 1, 1, 0};
static int yDir[] = new int[]{-1, 0, 1, 1, 1, 0, -1, -1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
StringTokenizer st = new StringTokenizer(br.readLine());
w = Integer.parseInt(st.nextToken());
h = Integer.parseInt(st.nextToken());
if (w == 0 && h == 0) {
break;
}
map = new int[h][w];
visted = new boolean[h][w];
answer = 0;
for (int i = 0; i < h; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < w; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (map[i][j] > 0 && !visted[i][j]) {
Queue<Integer> xQueue = new LinkedList<>();
Queue<Integer> yQueue = new LinkedList<>();
xQueue.offer(i);
yQueue.offer(j);
visted[i][j] = true;
while (!xQueue.isEmpty()) {
int x = xQueue.poll();
int y = yQueue.poll();
for (int k = 0; k < 8; k++) {
int nextX = x + xDir[k];
int nextY = y + yDir[k];
if (nextX >= 0 && nextX < h && nextY >= 0 && nextY < w) {
if (map[nextX][nextY] > 0 && !visted[nextX][nextY]) {
xQueue.offer(nextX);
yQueue.offer(nextY);
visted[nextX][nextY] = true;
}
}
}
}
answer++;
}
}
}
System.out.println(answer);
}
}
}