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

miinsun

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

Algorithm/Baekjoon

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

miinsun 2022. 3. 12. 17:16

 

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

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

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

 

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

์ž…๋ ฅ 

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

์ถœ๋ ฅ

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

 

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

2

 

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

2

 

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

9

 

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

55

โ€‹

 

 

โ€‹

๐Ÿ’ป  Main.java

  • DP๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ‘ผ๋‹ค. ๊ฐ๊ฐ์˜ ์—ฐ์‚ฐ์˜ ๋ฐฉ๋ฒ•์„ dy[n]์— ์ €์žฅํ•œ๋‹ค.
  • ํƒ€์ผ์˜ ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ทœ์น™์€ ํ”ผ๋ณด๋‚˜์น˜ ๊ณต์‹๊ณผ ๊ฐ™๋‹ค
  • dy[0]๊ณผ dy[1], dy[2]๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค.
/* ๋ฐฑ์ค€ DP - 11726 :: 2xn ํƒ€์ผ๋ง */
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[n + 2];
		
		dy[0] = 1;
		dy[1] = 1;
		dy[2] = 2;
		
		for(int i = 3; i <= n; i++) {
			dy[i] = (dy[i - 1] + dy[i - 2]) % 10007;
			
		}
		
		System.out.println(dy[n]);
		sc.close();
	}
}
Comments