Updated:

1. 문제 링크

https://www.acmicpc.net/problem/10844

2. 사용 알고리즘

DP

3. 풀이

d[n][a] : 길이가 n인 계단수의 개수, n번째 자리수는 a

  • 1 <= a <= 8 일 때, n - 1 자리에 가능한 수 : a - 1, a + 1

    • d[n][a] = d[n - 1][a - 1] + d[n - 1][a + 1]
  • a = 0 일 때, n - 1 자리에 가능한 수 : a + 1

    • d[n][a] = d[n - 1][a + 1]
  • a = 9 일 때, n - 1 자리에 가능한 수 : a - 1

    • d[n][a] = d[n - 1][a - 1]

∴ d[n] = d[n - 1][0] + d[n - 1][1] + … + d[n - 1][9]

4. 소스 코드

4-1. C++

4-1-1. Top-Down

https://github.com/dev-aiden/problem-solving/blob/main/boj/10844.cpp

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
#include <iostream>

using namespace std;

int d[103][13];

int solve(int n, int a) {
    int ret = 0;
    if (n == 1) {
        if (a == 0) return 0;
        else return 1;
    }
    if ((a - 1) >= 0) {
        if (d[n - 1][a - 1] == 0) d[n - 1][a - 1] = solve(n - 1, a - 1);
        ret += d[n - 1][a - 1];
        ret %= 1000000000;
    }
    if ((a + 1) <= 9) {
        if (d[n - 1][a + 1] == 0) d[n - 1][a + 1] = solve(n - 1, a + 1);
        ret += d[n - 1][a + 1];
        ret %= 1000000000;
    }
    return ret;
}

int main(void) {
    ios_base::sync_with_stdio(false);
    int n; cin >> n;
    int result = 0;
    for (int i = 0; i < 10; ++i) {
        result += solve(n, i);
        result %= 1000000000;
    }
    cout << result << "\n";
    return 0;
}

4-1-2. Bottom-Up

https://github.com/dev-aiden/problem-solving/blob/main/boj/10844_2.cpp

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
#include <iostream>

using namespace std;

int d[103][13];

int main(void) {
    ios_base::sync_with_stdio(false);
    int n; cin >> n;
    for (int i = 1; i < 10; ++i) d[1][i] = 1;
    for (int i = 2; i <= n; ++i) {
        for (int j = 0; j <= 9; ++j) {
            if (j - 1 >= 0) d[i][j] += d[i - 1][j - 1];
            if (j + 1 <= 9) d[i][j] += d[i - 1][j + 1];
            d[i][j] %= 1000000000;
        }
    }
    int result = 0;
    for (int i = 0; i <= 9; ++i) {
        result += d[n][i];
        result %= 1000000000;
    }
    cout << result << "\n";
    return 0;
}

4-2. JAVA

4-2-1. Top-Down

https://github.com/dev-aiden/problem-solving/blob/main/boj/10844.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    static int d[][] = new int[103][13];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int result = 0;
        for(int i = 0; i < 10; ++i) {
            result += solve(n, i);
            result %= 1000000000;
        }
        System.out.println(result);
    }

    public static int solve(int n, int a) {
        int ret = 0;
        if (n == 1) {
            if (a == 0) return 0;
            else return 1;
        }
        if ((a - 1) >= 0) {
            if (d[n - 1][a - 1] == 0) d[n - 1][a - 1] = solve(n - 1, a - 1);
            ret += d[n - 1][a - 1];
            ret %= 1000000000;
        }
        if ((a + 1) <= 9) {
            if (d[n - 1][a + 1] == 0) d[n - 1][a + 1] = solve(n - 1, a + 1);
            ret += d[n - 1][a + 1];
            ret %= 1000000000;
        }
        return ret;
    }
}

4-2-2. Bottom-Up

https://github.com/dev-aiden/problem-solving/blob/main/boj/10844_2.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    static int d[][] = new int[103][13];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        for (int i = 1; i < 10; ++i) d[1][i] = 1;
        for (int i = 2; i <= n; ++i) {
            for (int j = 0; j <= 9; ++j) {
                if (j - 1 >= 0) d[i][j] += d[i - 1][j - 1];
                if (j + 1 <= 9) d[i][j] += d[i - 1][j + 1];
                d[i][j] %= 1000000000;
            }
        }
        int result = 0;
        for (int i = 0; i <= 9; ++i) {
            result += d[n][i];
            result %= 1000000000;
        }
        System.out.println(result);
    }
}

Updated:

Leave a comment