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