본문 바로가기
백준

백준 동적계획법 - 9184번 : 신나는 함수 실행

by 맴썰 2022. 2. 16.

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

 

9184번: 신나는 함수 실행

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

www.acmicpc.net


import java.io.*;
import java.util.*;


public class Main {
		public static int[][][] memo = new int[101][101][101];
	public static void main(String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int a = 0; int b = 0; int c = 0;
		while(true) {
			st = new StringTokenizer(br.readLine());
			a = Integer.parseInt(st.nextToken());
			b = Integer.parseInt(st.nextToken());
			c = Integer.parseInt(st.nextToken());
			if(a==-1&&b==-1&&c==-1) break;
			System.out.println("w("+a+", "+b+", "+c+") = "+func(a,b,c));
		}
	}
	static int func(int a, int b, int c) {
		if(memo[a+50][b+50][c+50]==0) {
			if(a<=0||b<=0||c<=0) memo[a+50][b+50][c+50] = 1;
			else if(a>20||b>20||c>20) memo[a+50][b+50][c+50] = func(20,20,20);
			else if(a<b&&b<c) memo[a+50][b+50][c+50] = func(a,b,c-1)+ func(a,b-1,c-1)-func(a,b-1,c);
			else memo[a+50][b+50][c+50] = func(a-1,b,c)+func(a-1,b-1,c)+func(a-1,b,c-1)-func(a-1,b-1,c-1);
		}
		return memo[a+50][b+50][c+50];
	}
}