Repository navigation
Expand file tree
/
Copy pathMain_4963.java
More file actions
64 lines (51 loc) · 1.59 KB
/
Copy pathMain_4963.java
File metadata and controls
64 lines (51 loc) · 1.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
package baekjoon;
import java.util.Scanner;
public class Main_4963 {
static int[][] map;
static int[] dx = {0, 0, 1, -1, -1, 1, -1, 1};
static int[] dy = {1, -1, 0, 0, -1, 1, 1, -1};
static boolean[][] visited;
static int h;
static int w;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
while (true) {
w = sc.nextInt(); // 가로
h = sc.nextInt(); // 세로
if (w == 0 && h == 0) {
break;
}
map = new int[h][w];
visited = new boolean[h][w];
// 지도 입력
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
map[i][j] = sc.nextInt();
}
}
int result = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (map[i][j] == 1 && visited[i][j] == false) {
result++;
search(i, j);
}
}
}
System.out.println(result);
}
}
static void search(int x, int y) {
visited[x][y] = true;
for (int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if ((nx >= 0 && ny >= 0) && (nx < h && ny < w)) {
if (map[nx][ny] == 1 && visited[nx][ny] == false) {
// visited[nx][ny] = true;
search(nx, ny);
}
}
}
}
}