๋ฐ์ํ
https://www.acmicpc.net/problem/11727
์ฝ๋
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] dp = new int[1001];
dp[1] = 1;
dp[2] = 3;
for(int i=3; i<1001; i++)
dp[i] = (dp[i-1] + (2*dp[i-2])) % 10007;
System.out.println(dp[n]);
scan.close();
}
}
ํ์ด
์ ๋ฌธ์ ๋ 1x2 , 2x1 ํ์ผ๋ก ์ฑ์ฐ์ง๋ง ์ด ๋ฌธ์ ๋ 2x1 , 2x2 ํ์ผ๋ก ์ฑ์ฐ๋ ๋ฐฉ๋ฒ์ ์์ด๋ค.
2x1์ด๋ผ๊ณ ํด์ ๊ธธ์ญํ ์ธ๋ก ์ง์ฌ๊ฐํ๋ง ์๊ฐํ๋๋ฐ, ์ด ์ธ๋ก๋ฅผ ๋ํ๋ฉด ๊ฐ๋ก ์ง์ฌ๊ฐํ๋ ๊ฐ๋ฅํ๋ค.
๋ง์ง๋ง ๋ค๋ชจ๋ฅผ 2X1 ์ง์ฌ๊ฐํ ํ๋๋ก ๋๋ ๊ฒฝ์ฐ -> dp[n-1]
๋ง์ง๋ง ๋ค๋ชจ๋ฅผ 1X2 ์ง์ฌ๊ฐํ ๋๊ฐ๋ก ๋๋ ๊ฒฝ์ฐ -> dp[n-2]
๊ทธ๋ฆฌ๊ณ ์ถ๊ฐ์ ์ผ๋ก ๋ง์ง๋ง์ผ๋ก ๋ค๋ชจ๋ฅผ 2x2 ์ง์ฌ๊ฐํ ํ๊ฐ๋ก ๋๋๊ฒฝ์ฐ -> dp[n-2] ์ ๋ฐฉ๋ฒ์ด ์๋ค.
๋ฐ๋ผ์ ์ต์ข ์ ํ์์ ์๋์ ๊ฐ๋ค.
dp[n] = dp[n-1] + dp[n-2] + dp[n-2]
dp[n] = dp[n-1] + (2 * dp[n-2])
๋ฐ์ํ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค[Java] - ๋จ์์นด๋ฉ๋ผ(Greedy) (0) | 2020.03.02 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค[Java] - ์ง์ง์ด ์ ๊ฑฐํ๊ธฐ (0) | 2020.03.02 |
[Codeforces] 677A: Vanya and Fence (0) | 2020.03.01 |
[๋ฐฑ์ค] 4641๋ฒ: Doubles(์์ ํ์) (0) | 2020.02.29 |
[๋ฐฑ์ค] 2156๋ฒ: ํฌ๋์ฃผ ์์(DP) (0) | 2020.02.28 |
๋๊ธ