알고리즘

백준 17829번 222-풀링 (JAVA)

박카스마시며코딩 2023. 6. 5. 16:55

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

 

17829번: 222-풀링

조기 졸업을 꿈꾸는 종욱이는 요즘 핫한 딥러닝을 공부하던 중, 이미지 처리에 흔히 쓰이는 합성곱 신경망(Convolutional Neural Network, CNN)의 풀링 연산에 영감을 받아 자신만의 풀링을 만들고 이를 22

www.acmicpc.net

 

저는 재귀를 통해 문제를 구현했습니다.

재귀를 통해 들어가고 4개의 값을 우선순위 큐로 받고 하나를 뺴고 그 다음 것을 리턴하도록 하여 구현하였습니다.

 

package BOJ.recursion;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_17829 {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Function<String,Integer> stoi = Integer::parseInt;
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = stoi.apply(st.nextToken());
        int[][] input = new int[n][n];
        for(int i = 0 ; i < n ; i++){
            st = new StringTokenizer(br.readLine());
            for(int j = 0 ; j < n ; j++){
                input[i][j] = stoi.apply(st.nextToken());
            }
        }
        int result = cal(0,0,input,n,n);
        System.out.println(result);
    }

    private static int cal(int y, int x, int[][] input, int length, int n) {
        if(length == 1){
            return input[y][x];
        }
        int nextLength = length / 2;
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        for(int i = 0 ; i < 2 ; i++){
            for(int j = 0 ; j < 2 ; j++){
                pq.offer(cal(y+nextLength*i,x+nextLength*j,input,nextLength,n));
            }
        }
        pq.poll();
        return pq.poll();
    }
}