DP로 풀었다.


날짜 : depth

현 세대 : x

다음 세대 : y


다른 코드들과 비교해보니 시간차이가 많이 난다. 나중에 다시 풀어봐야겠다.


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
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
 
typedef long long ll;
int h, w;
ll d[6][201][201], mod = 1000000007;
 
// 현 세대 x, 다음 세대 y마리
ll dp(int depth, int x, int y) {
    if (depth == h) {
        return y ? 0 : 1;
    }
    if (!x) {
        return y ? dp(depth + 1, y, 0) : 0;
    }
    if (d[depth][x][y] != -1return d[depth][x][y];
    ll ans = 0;
    for (int i = 0; y + i <= w; i++) {
        ans += dp(depth, x - 1, y + i);
        ans %= mod;
    }
    d[depth][x][y] = ans%mod;
    return d[depth][x][y];
}
 
int main() {
    memset(d, -1sizeof(d));
    scanf("%d%d"&h, &w);
    if (!h) printf("1");
    else printf("%lld", dp(010)%mod);
 
    return 0;
}
cs


'BOJ' 카테고리의 다른 글

1371 가장 많은 글자  (0) 2018.02.17
14950 정복자  (0) 2018.02.17
2855 흥미로운 수열  (0) 2017.12.14
14948 군대탈출하기  (0) 2017.12.14
14947 상자 배달  (0) 2017.12.14

+ Recent posts