์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[๋ฐฑ์ค€(BOJ)] 11726๋ฒˆ: 2×n ํƒ€์ผ๋ง (Dynamic Programming, C++)

PROGRAMMING/Algorithm

by koharin 2021. 2. 11. 12:47

๋ณธ๋ฌธ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ป C++ ์ œ์ถœ์ฝ”๋“œ


#include <bits/stdc++.h>

using namespace std;

int main(){
    int n;
    
    scanf("%d", &n);
    
    vector<int> DP(n);
    DP[0] = 1; DP[1] = 2;

    for(int i = 2; i < n; i++){
        DP[i] = (DP[i-2] + DP[i-1]) % 10007;
    }
    printf("%d", DP[n-1]);
}

 

๐Ÿ“ ๋ฌธ์ œํ’€์ด ๊ณผ์ •


2 x n ์ง์‚ฌ๊ฐํ˜•์€ ์ด์ „ ์ง์‚ฌ๊ฐํ˜•์—์„œ 2 x 1 ์”ฉ ๋Š˜์–ด๋‚œ๋‹ค.

  • ๋งŒ์•ฝ, ์ด์ „ DP[n-1]์„ ๊ฐ€์ ธ์˜จ๋‹ค๋ฉด 2 x 1์„ ํ•˜๋‚˜ ๋†“์„ ์ˆ˜ ์žˆ๊ณ , DP[n-2]๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค๋ฉด 1 x 2๋ฅผ ๋‘ ๊ฐœ ๋†“๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ DP[n]์—์„œ์˜ ์ ํ™”์‹ DP[n] = DP[n-2] + DP[n-1]์ด ๋œ๋‹ค.
  • DP[n]์—๋Š” n-2, n-1์ด ํ•„์š”ํ•˜๋ฏ€๋กœ, DP[1] = 1, DP[2] = 1์„ ๋ฏธ๋ฆฌ ์„ ์–ธํ•ด๋†“๋Š”๋‹ค.

 

๐Ÿ‘ ๊ฒฐ๊ณผ


728x90
๋ฐ˜์‘ํ˜•

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ