BOJ_S2_9184
π [S2_9184] μ λλ ν¨μ μ€ν
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static StringTokenizer st;
static int[][][] dp;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
dp = new int[21][21][21];
while(true){
st = new StringTokenizer(br.readLine(), " ");
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
if(a == -1 && b == -1 && c == -1){
break;
}
sb.append("w(").append(a).append(", ").append(b).append(", ").append(c).append(") = ").append(w(a,b,c)).append("\n");
}
System.out.print(sb);
}
static int w(int a,int b,int c){
// μ΄λ―Έ μ μ₯λμλ κ²½μ°, λ²μμμ μμ κ²½μ° λ°λ‘ λ°ν
if(check(a,b,c) && dp[a][b][c] != 0){
return dp[a][b][c];
}
// μ¬κ·μ λ©λͺ¨λ¦¬μ μ΄μ
if(a <= 0 || b <=0 || c <= 0){
return 1;
}
else if(a > 20 || b > 20 || c >20){
return dp[20][20][20] = w(20,20,20);
}
else if(b > a && c > b){
return dp[a][b][c] = w(a,b,c-1) + w(a, b-1, c-1) - w(a, b-1 ,c);
}
else{
return dp[a][b][c] = w(a-1,b,c) + w(a-1,b-1,c) + w(a-1,b,c-1) - w(a-1,b-1,c-1);
}
}
// λ²μμμ μλμ§ νμΈ
static boolean check(int a, int b, int c){
return a >=0 && a<=20 && b>=0 && b<=20 && c>=0 && c<=20;
}
}
π€ λμ μκ°
DP λ¬Έμ μ΄λ€.
κΈ°λ³Έμ μΌλ‘ μ¬κ· ν¨μμ ꡬ쑰λ λμμκΈ° λλ¬Έμ λ©λͺ¨λ¦¬μ μ΄μ
λ§ ν΄μ£Όλ©΄ λλ€.
κ·Έλ¬λ ν κ°μ§ λ ν΄μΌν κ²μ΄ μμλ€. λ°λ‘ λ²μ μ€μ μ΄λ€.
a,b,c, μ€ νλλΌλ 20μ λμΌλ©΄ κ·Έλ₯ μ¬κ·ν¨μ w(20,20,20)μ λΆλ¬μ€κΈ° λλ¬Έμ
λ²μ μμ μκ±°λ μ΄λ―Έ λ©λͺ¨λ¦¬μ μ΄μ
λμ΄ μλ κ²μ λ°λ‘ νΈμΆμ ν΄μ£Όκ³
μλ κ²λ€μ κ³μ λ©λͺ¨λ¦¬μ μ΄μ
ν΄κ°λ κ³Όμ μ κ±°μΉλ€.