Updated:

1. 문제 링크

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

2. 사용 알고리즘

DP

3. 풀이

d[n] : 2 * n 크기의 직사각형을 채우는 방법의 수

  • 2 * (n - 2) 크기의 직사각형에 1 * 2 타일을 붙이면 2 * n 크기의 직사각형

  • 2 * (n - 1) 크기의 직사각형에 2 * 1 타일을 붙이면 2 * n 크기의 직사각형

∴ d[n] = d[n - 2] + d[n - 1]

4. 소스 코드

4-1. C++

4-1-1. Top-Down

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int d[1003];

int solve(int num) {
    if (num < 2) return 1;
    if (d[num] != 0) return d[num];
    return d[num] = (solve(num - 2) + solve(num - 1)) % 10007;
}

int main(void) {
    ios_base::sync_with_stdio(false);
    int n; cin >> n;
    cout << solve(n) << "\n";
    return 0;
}

4-1-2. Bottom-Up

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int d[1003];

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

4-2. JAVA

4-2-1. Top-Down

https://github.com/dev-aiden/problem-solving/blob/main/boj/11726.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    static int[] d = new int[1003];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        System.out.println(solve(n));
    }

    public static int solve(int num) {
        if (num < 2) return 1;
        if (d[num] != 0) return d[num];
        return d[num] = (solve(num - 2) + solve(num - 1)) % 10007;
    }
}

4-2-2. Bottom-Up

https://github.com/dev-aiden/problem-solving/blob/main/boj/11726_2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    static int[] d = new int[1003];

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

Updated:

Leave a comment