알고리즘

백준 17213번 과일 서리 (JAVA)

박카스마시며코딩 2023. 3. 12. 14:43

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

 

17213번: 과일 서리

민건이네 과일 농장은 N가지 종류의 과일을 재배하는 중이다. 평소 민건이에게 앙심을 품고 있던 지환이는 민건이를 골탕 먹이기 위하여 민건이네 과일 농장에서 과일들을 훔치기로 다짐했다.

www.acmicpc.net

 

 

 

package BOJ.etc;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.function.Function;

public class BOJ_17213 {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Function<String,Integer> stoi = Integer::parseInt;
        int n = stoi.apply(br.readLine());
        int m = stoi.apply(br.readLine());
        int[][] dp = new int[n+1][m+1];
        int result = cal(n,m,dp);
        System.out.println(result);
    }

    private static int cal(int depth, int num,int[][] dp) {
        if(depth == 0 && num == 0){
            return 1;
        }
        if(depth == 0 || num == 0){
            return 0;
        }
        if(dp[depth][num] != 0){
            return dp[depth][num];
        }
        int result = 0;
        for(int i = 1 ; i <= num ; i++){
            result += cal(depth-1,num-i,dp);
        }
        dp[depth][num] = result;
        return result;
    }
}