알고리즘/백준(BOJ)

백준 - 11726 2xn 타일링

시나모온 2020. 8. 14. 09:00

문제 링크입니다 : https://www.acmicpc.net/problem/11726

 

11726번: 2×n 타일링

2×n 크기의 직사각형을 1×2, 2×1 타일로 채우는 방법의 수를 구하는 프로그램을 작성하시오. 아래 그림은 2×5 크기의 직사각형을 채운 한 가지 방법의 예이다.

www.acmicpc.net

전형적인 DP 문제였다. 그리고 타일링 문제는 계단 오르기 문제랑 같이 DP에서 유명한 유형이다.

 

구현만 하면 되는 문제였다.

 

 

 

#include <iostream>
#include <vector>

using namespace std;

int n;
vector<int> cache;

int tiling(int index) {
    if(n - 1 == index) return 1;
    if(n - 2 == index) return 2;

    int& ret = cache[index];
    if(ret != -1) return ret;

    ret = (tiling(index + 1) + tiling(index + 2)) % 10007;

    return ret;
}

int main() {
    cin >> n;

    cache.assign(n, -1);

    cout << tiling(0);

    return 0;
}

 

 

개발 환경 : vscode

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~