https://www.acmicpc.net/problem/17829
저는 재귀를 통해 문제를 구현했습니다.
재귀를 통해 들어가고 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();
}
}
'알고리즘' 카테고리의 다른 글
백준 17845번 수강 과목 (JAVA) (0) | 2023.06.07 |
---|---|
프로그래머스 다항식 더하기 (JAVA) (0) | 2023.06.06 |
프로그래머스 23749번 카드컨트롤 (JAVA) (1) | 2023.06.04 |
백준 15993번 1,2,3 더하기 8 (JAVA) (0) | 2023.06.03 |
프로그래머스 1로 만들기 (JAVA) (0) | 2023.06.02 |