슈콩

[BOJ] 2468 안전 영역 본문

Algorithms/Baekjoon

[BOJ] 2468 안전 영역

shukong 2025. 11. 28. 22:22

 

 

[문제]

https://www.acmicpc.net/problem/2468

 

 

[소스 코드]

import java.io.*;
import java.util.*;

public class Main {
	static int[] dr = {-1,1,0,0};
	static int[] dc = {0,0,-1,1};
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int n = Integer.parseInt(br.readLine());
		int[][] map = new int[n][n];
		int max = Integer.MIN_VALUE;
		int min = Integer.MAX_VALUE;
		for(int i=0;i<n;i++) {
			st = new StringTokenizer(br.readLine());
			for(int j=0;j<n;j++) {
				map[i][j] = Integer.parseInt(st.nextToken());
				min = Math.min(min, map[i][j]);
				max = Math.max(max, map[i][j]);
			}
		}
		int result = 1;
		for(int h=min;h<=max;h++) {
			int cnt = 0;
			boolean[][] visit = new boolean[n][n];
			for(int i=0;i<n;i++) {
				for(int j=0;j<n;j++) {
					if(map[i][j]>h && !visit[i][j]) {
						cnt++;
						Queue<int[]> q = new LinkedList<>();
						q.offer(new int[] {i,j});
						visit[i][j] = true;
						while(!q.isEmpty()) {
							int[] curr = q.poll();
							for(int d=0;d<4;d++) {
								int nr = curr[0] + dr[d];
								int nc = curr[1] + dc[d];
								if(nr<0 || nr>=n || nc<0 || nc>=n) continue;
								if(map[nr][nc]>h && !visit[nr][nc]) {
									q.offer(new int[] {nr,nc});
									visit[nr][nc] = true;
								}
							}
						}
					}
				}
			}
			result = Math.max(result, cnt);
		}
		System.out.println(result);
	}
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 9205 맥주 마시면서 걸어가기  (0) 2025.11.29
[BOJ] 2573 빙산  (0) 2025.11.28
[BOJ] 5014 스타트링크  (0) 2025.11.28
[BOJ] 7569 토마토  (0) 2025.11.28
[BOJ] 2644 촌수계산  (0) 2025.11.28