|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.StringTokenizer; |
| 6 | + |
| 7 | +public class Main { |
| 8 | + private static int N; |
| 9 | + private static int M; |
| 10 | + private static int H; |
| 11 | + private static int[][] blocks; |
| 12 | + private static int[][] dp; |
| 13 | + private static final int MOD = 10007; |
| 14 | + public static void main(String[] args) throws IOException { |
| 15 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 16 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 17 | + |
| 18 | + N = Integer.parseInt(st.nextToken()); |
| 19 | + M = Integer.parseInt(st.nextToken()); |
| 20 | + H = Integer.parseInt(st.nextToken()); |
| 21 | + |
| 22 | + blocks = new int[N][]; |
| 23 | + for (int i = 0; i < N; i++) { |
| 24 | + st = new StringTokenizer(br.readLine()); |
| 25 | + int[] temp = new int[st.countTokens()]; |
| 26 | + for (int j = 0; j < temp.length; j++) { |
| 27 | + temp[j] = Integer.parseInt(st.nextToken()); |
| 28 | + } |
| 29 | + blocks[i] = temp; |
| 30 | + } |
| 31 | + |
| 32 | + dp = new int[N + 1][H + 1]; |
| 33 | + for (int i = 0; i <= N; i++) { |
| 34 | + for (int j = 0; j <= H; j++) { |
| 35 | + dp[i][j] = -1; |
| 36 | + } |
| 37 | + } |
| 38 | + System.out.println(dfs(0, 0)); |
| 39 | + } |
| 40 | + |
| 41 | + private static int dfs(int i, int h) { |
| 42 | + if (h > H) return 0; |
| 43 | + if (i == N) return h == H ? 1 : 0; |
| 44 | + if (dp[i][h] != -1) return dp[i][h]; |
| 45 | + int result = dfs(i + 1, h); |
| 46 | + for (int b : blocks[i]) { |
| 47 | + if (h + b <= H) { |
| 48 | + result += dfs(i + 1, h + b); |
| 49 | + //맨 마지막에 나누지말고 미리 빼버리기 |
| 50 | + if (result >= MOD) |
| 51 | + result -= MOD; |
| 52 | + } |
| 53 | + } |
| 54 | + dp[i][h] = result; |
| 55 | + return result; |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
0 commit comments