01-10 13:07
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ DP 11727 :: 2xn ํƒ€์ผ๋ง 2 JAVA ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ DP 11727 :: 2xn ํƒ€์ผ๋ง 2 JAVA

miinsun 2022. 3. 12. 20:28

 

๐Ÿ’ฌ  ๋ฌธ์ œ ์„ค๋ช…

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

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

 

๐Ÿ”จ  ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ 

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

์ถœ๋ ฅ

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

 

์˜ˆ์ œ ์ž…๋ ฅ 1)

2

 

์˜ˆ์ œ ์ถœ๋ ฅ 1)

3

 

์˜ˆ์ œ ์ž…๋ ฅ 2)

8

 

์˜ˆ์ œ ์ถœ๋ ฅ 2)

171

โ€‹

์˜ˆ์ œ ์ž…๋ ฅ 3)

12

 

์˜ˆ์ œ ์ถœ๋ ฅ 3)

2731

โ€‹

 

 

โ€‹

๐Ÿ’ป  Main.java

  • DP๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ‘ผ๋‹ค. ๊ฐ๊ฐ์˜ ์—ฐ์‚ฐ์˜ ๋ฐฉ๋ฒ•์„ dy[n]์— ์ €์žฅํ•œ๋‹ค.
  • ํƒ€์ผ์˜ ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ทœ์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. dy[i] = dy[i-1] + 2 * dy[i-2]
  • dy[0]๊ณผ dy[1], dy[2]๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค.
/* ๋ฐฑ์ค€ DP - 11727 :: 2xn ํƒ€์ผ๋ง 2 */
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] dy = new int[1001];
		
		dy[1] = 1; 
        dy[2] = 3;
        
    	for(int i = 3; i <= n; i++){
    		dy[i] = (dy[i-1] + 2 * dy[i-2]) % 10007;
    	}
    	

        System.out.println(dy[n]);

		sc.close();
	} 
}
Comments