상세 컨텐츠

본문 제목

백준 9184번

백준 알고리즘 풀이

by 발발개발 2021. 1. 20. 18:33

본문

원본 : www.acmicpc.net/problem/9184

 

9184번: 신나는 함수 실행

입력은 세 정수 a, b, c로 이루어져 있으며, 한 줄에 하나씩 주어진다. 입력의 마지막은 -1 -1 -1로 나타내며, 세 정수가 모두 -1인 경우는 입력의 마지막을 제외하면 없다.

www.acmicpc.net

 

풀이

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

public class Main {
    
    public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
    
    public static void main(String[] args) throws Exception {
        
        int result[][][] = new int[21][21][21];
        
        ArrayList<String> list = new ArrayList<String>();
        while(true) {
            String str = reader.readLine();
            if (str.equals("-1 -1 -1")) {
                break;
            } else {
                int a = Integer.parseInt(str.split(" ")[0]);
                int b = Integer.parseInt(str.split(" ")[1]);
                int c = Integer.parseInt(str.split(" ")[2]);
                list.add("w(" + a + ", " + b + ", " + c + ") = " + w(a, b, c, result));
            }
        }
        
        for (int i = 0; i < list.size(); i++) {
            writer.write(list.get(i) + "\n");
        }
        
        writer.flush();
    }
    
    public static int w(int a, int b, int c, int result[][][]) {
        if (a <= 0 || b <= 0 || c <= 0) {
            return 1;
        } else if (a > 20 || b > 20 || c > 20) {
            return w(20, 20, 20, result);
        } else if (result[a][b][c] == 0) {
            if (a < b && b < c) {
                return result[a][b][c] = w(a, b, c - 1, result) + w(a, b - 1, c - 1, result) - w(a, b - 1, c, result);
            } else {
                return result[a][b][c] = w(a - 1, b, c, result) + w(a - 1, b - 1, c, result) + w(a - 1, b, c - 1, result) - w(a - 1, b - 1, c - 1, result);
            }
        } else {
            return result[a][b][c];
        }
    }
    
}

'백준 알고리즘 풀이' 카테고리의 다른 글

백준 1149번  (0) 2021.01.26
백준 9461번  (0) 2021.01.25
백준 1003번  (0) 2021.01.20
백준 14889번  (0) 2021.01.19
백준 1931번  (0) 2021.01.19

관련글 더보기

댓글 영역