17829번: 222-풀링
조기 졸업을 꿈꾸는 종욱이는 요즘 핫한 딥러닝을 공부하던 중, 이미지 처리에 흔히 쓰이는 합성곱 신경망(Convolutional Neural Network, CNN)의 풀링 연산에 영감을 받아 자신만의 풀링을 만들고 이를 22
www.acmicpc.net
평범하게 범위를 반씩 나눠가며, 총 네개의 원소가 남을때까지 반복하는 분할 정복 문제입니다.
문제 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class boj17829 {
int[][] matrix;
public int solution() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
matrix = new int[n][n];
for (int i = 0; i < n; i++) {
StringTokenizer row = new StringTokenizer(reader.readLine());
for (int j = 0; j < n; j++) {
matrix[j][i] = Integer.parseInt(row.nextToken());
}
}
return pooling(n, 0, 0);
}
public int pooling(int n, int x, int y) {
if (n == 2) {
int[] four = new int[]{
matrix[y][x],
matrix[y + 1][x],
matrix[y][x + 1],
matrix[y + 1][x + 1]
};
Arrays.sort(four);
return four[2];
}
else {
int half = n / 2;
int[] four = new int[]{
pooling(half, x, y),
pooling(half, x + half, y),
pooling(half, x, y + half),
pooling(half, x + half, y + half)
};
Arrays.sort(four);
return four[2];
}
}
public static void main(String[] args) throws IOException {
System.out.println(new boj17829().solution());
}
}
출력결과
입력
4
-6 -8 7 -4
-5 -5 14 11
11 11 -1 -1
4 9 -2 -4
출력
11
'Algorithm' 카테고리의 다른 글
백준 15903 카드 합체 놀이 (0) | 2023.07.13 |
---|---|
백준 2447 별찍기10 (0) | 2023.07.12 |
이진 탐색 알고리즘 (0) | 2023.07.11 |
퀵 정렬(Quicksort) 알고리즘 (0) | 2023.07.11 |
백준 1992 쿼드트리 (0) | 2023.07.11 |