Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- binary search
- greedy
- 시뮬레이션
- 코테
- BFS
- 러닝일지
- SWEA
- 프로그래머스
- oracle
- 문제해결
- 코딩테스트
- 운동기록
- 건강
- Java
- COS PRO
- 문제풀이
- BOJ
- 자바
- 알고리즘
- math
- dfs
- 백준
- 문자열
- db
- MySQL
- SQL
- DP
- 이분탐색
- 투포인터
- priorityqueue
Archives
- Today
- Total
슈콩
[BOJ] 백준 2468 안전 영역 본문
[문제]
https://www.acmicpc.net/problem/2468
[소스 코드]
import java.io.*;
import java.util.*;
public class Main {
static int n,result = 0;
static int[][] map;
static int[] dr = {-1,1,0,0};
static int[] dc = {0,0,-1,1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
n = Integer.parseInt(br.readLine());
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());
max = Math.max(max, map[i][j]);
min = Math.min(min, map[i][j]);
}
}
for(int h=min-1;h<max;h++) {
result = Math.max(result,bfs(h));
}
System.out.println(result);
}
private static int bfs(int h) {
Queue<int[]> q = new LinkedList<>();
boolean[][] visit = new boolean[n][n];
int cnt = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(map[i][j]>h && !visit[i][j]) {
visit[i][j] = true;
q.offer(new int[] {i,j});
cnt++;
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 || visit[nr][nc]) continue;
if(map[nr][nc]>h) {
visit[nr][nc] = true;
q.offer(new int[] {nr,nc});
}
}
}
}
}
}
return cnt;
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 백준 9205 맥주 마시면서 걸어가기 (0) | 2025.09.30 |
|---|---|
| [BOJ] 백준 2573 빙산 (0) | 2025.09.30 |
| [BOJ] 백준 5014 스타트링크 (0) | 2025.09.30 |
| [BOJ] 백준 1697 숨바꼭질 (0) | 2025.09.30 |
| [BOJ] 백준 7569 토마토 (0) | 2025.09.29 |