Algorithm๐Ÿฅ‡

11727.2xnํƒ€์ผ๋ง2

hae02y 2023. 11. 1. 00:47
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

2×n ์ง์‚ฌ๊ฐํ˜•์„ 1×2, 2×1๊ณผ 2×2 ํƒ€์ผ๋กœ ์ฑ„์šฐ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์•„๋ž˜ ๊ทธ๋ฆผ์€ 2×17 ์ง์‚ฌ๊ฐํ˜•์„ ์ฑ„์šด ํ•œ๊ฐ€์ง€ ์˜ˆ์ด๋‹ค.

## ์ž…๋ ฅ

์ฒซ์งธ ์ค„์— n์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ n ≤ 1,000)

## ์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— 2×n ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜•์„ ์ฑ„์šฐ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ 10,007๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์‹œ๊ฐ„ ์ œํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ ์ œ์ถœ ์ •๋‹ต ๋งžํžŒ ์‚ฌ๋žŒ ์ •๋‹ต ๋น„์œจ
1 ์ดˆ 256 MB 68465 40719 32702 58.894%

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

ํ’€์ด

์ฝ”๋“œ

import java.util.Scanner;  

public class _2xnํƒ€์ผ๋ง2 {  

    static int[] dp = new int[1001];  

    public static void main(String[] args) {  
        Scanner sc = new Scanner(System.in);  

        int n = sc.nextInt();  

        System.out.println(tiling(n));  
        sc.close();  
    }  

    public static int tiling(int n){  

        if(n == 1) return 1;  
        if(n == 2) return 3;  

        if(dp[n] == 0){  
            dp[n] = (tiling(n-1) + (tiling(n-2) * 2)) % 10007;  
        }  
        return dp[n];  
    }  
}
๋ฐ˜์‘ํ˜•