[BOJ] 18427 함께 블록 쌓기

Time Lapse :25min 4sec

18427.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.*;
import java.util.*;
import java.util.concurrent.Semaphore;


public class Main {
static int n, m, h;
static int[][] dp = new int[51][1001];
static List<Integer> l[] = new ArrayList[51];
public static void main( String[] argc ) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
h = sc.nextInt();
sc.nextLine();
for(int i = 0; i < n; i++) {
l[i] = new ArrayList<>();
StringTokenizer st = new StringTokenizer(sc.nextLine());
while(st.hasMoreTokens()) {
l[i].add(Integer.parseInt(st.nextToken()));
}
dp[i][0] = 1;
for(int j = 1; j <= h; j++)
dp[i][j] = -1;
}
System.out.println(dpf(0, h));
}
static int dpf(int cur, int left) {
if(left == 0) return 1;
if(left < 0) return 0;
if(dp[cur][left]!=-1) return dp[cur][left];
if(cur == n) return 0;
dp[cur][left] = dpf(cur + 1,left);
for(int possible : l[cur]) {
dp[cur][left] += dpf(cur+1, left - possible);
}
dp[cur][left] %= 10007;
return dp[cur][left];
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/09/07/PS/BOJ/18427/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.