[자바] SWEA 7733 : 치즈 도둑 (D4)
풀이
방향성 생각
- BFS + 완탐 문제
- 각 날짜마다 BFS를 돌려서 몇 덩어리가 나오는지 체크하기.
- 초기값을 0으로 초기화한 경우 저격 테케가 있다.
- 항상 초기값 체크 신경써서 할당하기
전체코드
import java.util.*;
import java.io.*;
class Solution {
static int N;
static int[][] arr;
static boolean[][] V;
static int[][] dires = {{1,0},{0,1},{-1,0},{0,-1}};
static boolean inside(int x, int y) {
return 0 <= x && x < N && 0 <= y && y < N;
}
static void bfs(int sx, int sy, int day) {
ArrayDeque<int[]> Q = new ArrayDeque<>();
Q.offer(new int[]{sx, sy});
V[sy][sx] = true;
while (!Q.isEmpty()) {
int[] cur = Q.poll();
int x = cur[0], y = cur[1];
for (int[] dire : dires) {
int nx = x + dire[0], ny = y + dire[1];
if (inside(nx, ny) && arr[ny][nx] > day && !V[ny][nx]) {
Q.offer(new int[]{nx, ny});
V[ny][nx] = true;
}
}
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int TC = Integer.parseInt(br.readLine());
for (int tc = 1; tc <= TC; tc++) {
N = Integer.parseInt(br.readLine());
arr = new int[N][N];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
int answer = 1;
for (int day = 1; day <= 100; day++) {
V = new boolean[N][N];
int count = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (arr[i][j] > day && !V[i][j]) {
bfs(j, i, day);
count++;
}
}
}
answer = Math.max(answer, count);
}
System.out.println("#" + tc + " " + answer);
}
}
}
코멘트
- .
'Algorithm > Graph' 카테고리의 다른 글
[파이썬] 백준 18133 : 가톨릭대학교에 워터 슬라이드를?? (플레4) (0) | 2025.03.09 |
---|---|
[파이썬] 백준 26146 : 즉흥 여행 (easy) (플레5) (0) | 2025.03.09 |
[파이썬] SWEA 1267 : 작업 순서 (test) (0) | 2025.03.09 |
[파이썬] SWEA 1247 : 최적 경로 (D5) (0) | 2025.03.09 |
[파이썬, 자바] 백준 1506 : 경찰서 (플레5) (0) | 2025.03.09 |
[파이썬, 자바] 백준 4196 : 도미노 (플레4) (0) | 2025.03.08 |
[파이썬] 백준 2150 : Strongly Connected Component (플레5) (0) | 2025.03.07 |
댓글