https://www.acmicpc.net/problem/1926
1. 아이디어
BFS와 DFS를 활용해서 간단하게 해결할 수 있다.
2. 문제풀이
입력을 받은 후 2중 for문으로 도화지를 순회하며 그림을 발견하면 BFS 또는 DFS 알고리즘으로 그림의 크기를 구했다. 이후 개수를 세주고 그림의 최대 크기를 저장하는 max 변수와 비교 갱신하는 방식으로 구현했다.
3. 코드
import java.io.*;
import java.util.*;
public class Main_BFS {
private static final int[] dr = {-1, 0, 1, 0};
private static final int[] dc = {0, 1, 0, -1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[][] map = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
boolean[][] visited = new boolean[N][M];
int cnt = 0;
int max = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j] == 0 || visited[i][j]) continue;
int result = bfs(i, j, N, M, map, visited);
max = Math.max(max, result);
cnt++;
}
}
System.out.println(cnt);
System.out.println(max);
}
private static int bfs(int r, int c, int N, int M, int[][] map, boolean[][] visited) {
Queue<int[]> q = new ArrayDeque<>();
q.offer(new int[]{r, c});
visited[r][c] = true;
int cnt = 0;
while (!q.isEmpty()) {
int[] node = q.poll();
cnt++;
for (int dir = 0; dir < 4; dir++) {
int nr = node[0] + dr[dir];
int nc = node[1] + dc[dir];
if (nr < 0 || nr >= N || nc < 0 || nc >= M) continue;
if (map[nr][nc] == 0 || visited[nr][nc]) continue;
q.offer(new int[]{nr, nc});
visited[nr][nc] = true;
}
}
return cnt;
}
}
import java.io.*;
import java.util.*;
public class Main_DFS {
private static final int[] dr = {-1, 0, 1, 0};
private static final int[] dc = {0, 1, 0, -1};
private static int N;
private static int M;
private static int[][] map;
private static boolean[][] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
visited = new boolean[N][M];
int cnt = 0;
int max = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j] == 0 || visited[i][j]) continue;
int result = dfs(i, j);
max = Math.max(max, result);
cnt++;
}
}
System.out.println(cnt);
System.out.println(max);
}
private static int dfs(int r, int c) {
visited[r][c] = true;
int cnt = 1;
for (int dir = 0; dir < 4; dir++) {
int nr = r + dr[dir];
int nc = c + dc[dir];
if (nr < 0 || nr >= N || nc < 0 || nc >= M) continue;
if (map[nr][nc] == 0 || visited[nr][nc]) continue;
cnt += dfs(nr, nc);
}
return cnt;
}
}
4. 후기
- BFS
- DFS
'코딩테스트 준비 > 백준' 카테고리의 다른 글
[백준] 13460번 - 구슬 탈출 2 [Java] (0) | 2025.01.17 |
---|---|
[백준] 13459번 - 구슬 탈출 [Java] (0) | 2025.01.17 |
[백준] 2667번 - 단지번호붙이기 [Java] (0) | 2025.01.17 |
[백준] 2606번 - 바이러스 [Java] (0) | 2025.01.17 |
[백준] 7562번 - 나이트의 이동 [Java] (0) | 2025.01.17 |