728x90
https://www.acmicpc.net/problem/4963
메모리: 16,044 KB , 시간: 152 ms
사용 알고리즘: 그래프 이론, 그래프 탐색, 너비 우선 탐색
728x90
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int[] dx = {-1, 1, 0, 0, -1, -1, 1, 1};
static int[] dy = {0, 0, -1, 1, -1, 1, -1, 1};
static int w, h;
static int[][] map;
static boolean[][] visited;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder result = new StringBuilder();
st = new StringTokenizer(br.readLine());
w = Integer.parseInt(st.nextToken());
h = Integer.parseInt(st.nextToken());
int count;
while(w != 0 || h != 0) {
map = new int[h][w];
visited = new boolean[h][w];
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());
}
}
count = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if(map[i][j] == 1 && !visited[i][j]) {
bfs(i, j);
count++;
}
}
}
result.append(count).append("\n");
st = new StringTokenizer(br.readLine());
w = Integer.parseInt(st.nextToken());
h = Integer.parseInt(st.nextToken());
}
System.out.print(result);
}
private static void bfs(int x, int y) {
Queue<int[]> q = new ArrayDeque<>();
q.add(new int[] {x, y});
visited[x][y] = true;
int[] now;
int nx, ny;
while(!q.isEmpty()) {
now = q.poll();
for (int i = 0; i < 8; i++) {
nx = now[0] + dx[i];
ny = now[1] + dy[i];
if(nx >= 0 && nx < h && ny >= 0 && ny < w) {
if(map[nx][ny] == 1 && !visited[nx][ny]) {
visited[nx][ny] = true;
q.add(new int[] {nx, ny});
}
}
}
}
}
}
728x90
'Problem Solving > BOJ' 카테고리의 다른 글
[백준, BOJ 21921] 블로그 (java) (1) | 2024.11.07 |
---|---|
[백준, BOJ 15655] N과 M (6) (java) (0) | 2024.11.07 |
[백준, BOJ 20920] 영단어 암기는 괴로워 (java) (0) | 2024.11.04 |
[백준, BOJ 9017] 크로스 컨트리 (java) (0) | 2024.10.27 |
[백준, BOJ 13305] 주유소 (java) (0) | 2024.10.27 |