Skip to content

Commit 286f6bf

Browse files
committed
issue #42 9184
1 parent 398e922 commit 286f6bf

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

src/backjoon/_9184.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package backjoon;
2+
// https://www.acmicpc.net/problem/9184
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class _9184 {
9+
// 함수 w는 0이하는 1을 호출하고 20이상은 20을 호출하기때문에 길이는 21로 설정
10+
static int[][][] dp = new int[21][21][21];
11+
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
StringBuilder sb = new StringBuilder();
15+
// memory 24808 runtime 228
16+
while(true) {
17+
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
18+
19+
int a = Integer.parseInt(st.nextToken());
20+
int b = Integer.parseInt(st.nextToken());
21+
int c = Integer.parseInt(st.nextToken());
22+
23+
// -1 -1 -1 이 나오면 종료
24+
if (a == -1 && b == -1 && c == -1) {
25+
break;
26+
}
27+
sb.append("w(" + a + ", " + b + ", " + c + ") = ").append(w(a ,b ,c)).append('\n');
28+
}
29+
System.out.println(sb);
30+
}
31+
32+
static int w(int a, int b, int c) {
33+
34+
// 이미 저장되어있는 값은 그대로 호출
35+
if(inRange(a, b, c) && dp[a][b][c] != 0) {
36+
return dp[a][b][c];
37+
}
38+
39+
if(a <= 0 || b <= 0 || c <= 0) {
40+
return 1;
41+
}
42+
43+
if(a > 20 || b > 20 || c > 20) {
44+
return dp[20][20][20] = w(20, 20, 20);
45+
}
46+
47+
if(a < b && b < c) {
48+
return dp[a][b][c] = w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c);
49+
}
50+
51+
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);
52+
}
53+
54+
// a,b,c의 범위가 0 ~ 20 사이인지 체크
55+
static boolean inRange(int a, int b, int c) {
56+
return 0 <= a && a <= 20 && 0 <= b && b <= 20 && 0 <= c && c <= 20;
57+
}
58+
}
59+
/*
60+
input
61+
1 1 1
62+
2 2 2
63+
10 4 6
64+
50 50 50
65+
-1 7 18
66+
-1 -1 -1
67+
68+
output
69+
w(1, 1, 1) = 2
70+
w(2, 2, 2) = 4
71+
w(10, 4, 6) = 523
72+
w(50, 50, 50) = 1048576
73+
w(-1, 7, 18) = 1
74+
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy