๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[๋ฐฑ์ค€] 11729๋ฒˆ: ํ•˜๋…ธ์ด ํƒ‘ ์ด๋™ ์ˆœ์„œ(์žฌ๊ท€, ๋ถ„ํ• ์ •๋ณต)

by ์ฃผ๋ฐœ2 2020. 3. 11.
๋ฐ˜์‘ํ˜•

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

 

 

11729๋ฒˆ: ํ•˜๋…ธ์ด ํƒ‘ ์ด๋™ ์ˆœ์„œ

์„ธ ๊ฐœ์˜ ์žฅ๋Œ€๊ฐ€ ์žˆ๊ณ  ์ฒซ ๋ฒˆ์งธ ์žฅ๋Œ€์—๋Š” ๋ฐ˜๊ฒฝ์ด ์„œ๋กœ ๋‹ค๋ฅธ n๊ฐœ์˜ ์›ํŒ์ด ์Œ“์—ฌ ์žˆ๋‹ค. ๊ฐ ์›ํŒ์€ ๋ฐ˜๊ฒฝ์ด ํฐ ์ˆœ์„œ๋Œ€๋กœ ์Œ“์—ฌ์žˆ๋‹ค. ์ด์ œ ์ˆ˜๋„์Šน๋“ค์ด ๋‹ค์Œ ๊ทœ์น™์— ๋”ฐ๋ผ ์ฒซ ๋ฒˆ์งธ ์žฅ๋Œ€์—์„œ ์„ธ ๋ฒˆ์งธ ์žฅ๋Œ€๋กœ ์˜ฎ๊ธฐ๋ ค ํ•œ๋‹ค. ํ•œ ๋ฒˆ์— ํ•œ ๊ฐœ์˜ ์›ํŒ๋งŒ์„ ๋‹ค๋ฅธ ํƒ‘์œผ๋กœ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ์Œ“์•„ ๋†“์€ ์›ํŒ์€ ํ•ญ์ƒ ์œ„์˜ ๊ฒƒ์ด ์•„๋ž˜์˜ ๊ฒƒ๋ณด๋‹ค ์ž‘์•„์•ผ ํ•œ๋‹ค. ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์ด๋™ ์ˆœ์„œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. ๋‹จ, ์ด๋™ ํšŸ์ˆ˜๋Š” ์ตœ์†Œ๊ฐ€ ๋˜์–ด์•ผ ํ•œ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์€ ์›ํŒ์ด 5

www.acmicpc.net

์ฝ”๋“œ

import java.util.Scanner;

public class Main {
	static StringBuilder sb = new StringBuilder();
	static int count = 0;
	
	/* 
	 * ๊ธฐ๋‘ฅ A(from)์—์„œ N๊ฐœ์˜ ์›๋ฐ˜์„ ๊ธฐ๋‘ฅ B(other)๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ธฐ๋‘ฅ C(to)๋กœ ์˜ฎ๊ธฐ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜.
	 * N: ์›ํŒ์˜ ๊ฐœ์ˆ˜
	 * from: ์ถœ๋ฐœ์ง€ - A
	 * other: ์ถœ๋ฐœ์ง€๋„ ๋ชฉ์ ์ง€๋„ ์•„๋‹Œ ๊ณณ - B
	 * to: ๋ชฉ์ ์ง€ - C
	 *  1. ๊ธฐ๋‘ฅ A์—์„œ N-1๊ฐœ์˜ ์›๋ฐ˜์„ ๊ธฐ๋‘ฅ C๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ธฐ๋™ B๋กœ ์˜ฎ๊ธด๋‹ค.
	 *  2. ๊ธฐ๋‘ฅ A์—์„œ 1๊ฐœ์˜ ์›๋ฐ˜์„ ๊ธฐ๋‘ฅ C๋กœ ์˜ฎ๊ธด๋‹ค.
	 *  3. ๊ธฐ๋‘ฅ B์—์„œ N-1๊ฐœ์˜ ์›๋ฐ˜์„ ๊ธฐ๋‘ฅ A๋ฅผ ์ด์šฉํ•ด์„œ ๊ธฐ๋‘ฅ C๋กœ ์˜ฎ๊ธด๋‹ค.
	 */
	public static void hanoi(int N, int from, int other, int to) {
		if(N == 0)
			return;
		count ++;
		// n-1๊ฐœ์˜ ์›ํŒ์„ ๋ชฉ์ ์ง€๊ฐ€ ์•„๋‹Œ ๊ณณ(other)๋กœ ์˜ฎ๊ฒจ๋†“์Œ.
		hanoi(N-1, from, to, other);	
		
		// ๋งˆ์ง€๋ง‰ ์›ํŒ์„ ๋ชฉ์ ์ง€๋กœ ์˜ฎ๊น€.
		sb.append(from + " " + to + "\n");
		
		// ๋ชฉ์ ์ง€๊ฐ€ ์•„๋‹Œ ๊ณณ(other)์— ์˜ฎ๊ฒจ๋†“์•˜๋˜ ์›ํŒ๋“ค์„ ๋ชฉ์ ์ง€๋กœ ์˜ฎ๊น€
		hanoi(N-1, other, from, to);	
	}

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		hanoi(N, 1, 2, 3);
		System.out.println(count);
		System.out.println(sb);
		scan.close();
	}

}

ํ’€์ด

https://m.blog.naver.com/PostView.nhn?blogId=puri8467&logNo=221440092130&proxyReferer=https%3A%2F%2Fwww.google.com%2F

์žฌ๊ท€ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ๋ฌธ์ œ๋กœ ์œ ๋ช…ํ•œ ํ•˜๋…ธ์ด์˜ ํƒ‘๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์•˜๋‹ค.

์žฌ๊ท€ํ•จ์ˆ˜๊ฐ€ ๋ญ”๊ฐ€์š”? ๋ผ๋Š” ์ข‹์€ ์žฌ๊ท€ ๋ฌธ์ œ๋„ ์žˆ๋‹ค.

 

์ผ๋‹จ ํ•˜๋…ธ์ด ํƒ‘ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 1. ํ•œ ๋ฒˆ์— ํ•œ ๊ฐœ์˜ ์›ํŒ๋งŒ์„ ๋‹ค๋ฅธ ํƒ‘์œผ๋กœ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋‹ค.

 2. ์Œ“์•„ ๋†“์€ ์›ํŒ์€ ํ•ญ์ƒ ์œ„์˜ ๊ฒƒ์ด ์•„๋ž˜์˜ ๊ฒƒ๋ณด๋‹ค ์ž‘์•„์•ผ ํ•œ๋‹ค.

 

ํ•˜๋…ธ์ด ํƒ‘ ๋ฌธ์ œ์˜ ์›๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

https://m.blog.naver.com/PostView.nhn?blogId=puri8467&logNo=221440092130&proxyReferer=https%3A%2F%2Fwww.google.com%2F

์œ„ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด N๊ฐœ์˜ ์›ํŒ์ด ์กด์žฌํ•  ๋•Œ, 

public static void hanoi(int N, int from, int other, int to)

 1. N-1 ๊ฐœ์˜ ์›ํŒ์„ ๋‘ ๋ฒˆ์งธ ์นธ(์ค‘๊ฐ„์ง€์ )์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

  -> hanoi(N-1, from, to, other)

 2. ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋‚จ์€ ์›ํŒ 1๊ฐœ๋ฅผ ์„ธ ๋ฒˆ์งธ ์นธ(๋ชฉ์ ์ง€)์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

  -> move(from, to)

 3. ๋‘ ๋ฒˆ์งธ ์นธ(์ค‘๊ฐ„์ง€์ )์— ์กด์žฌํ•˜๋Š” N-1๊ฐœ์˜ ์›ํŒ์„ ์„ธ ๋ฒˆ์งธ ์นธ(๋ชฉ์ ์ง€)์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

  -> hanoi(N-1, other, from, to)

 

์œ„ ์žฌ๊ท€ํ•จ์ˆ˜๋Š” ์ง๊ด€์ ์œผ๋กœ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค.

hanoi(N-1, from, to, other) 

 -> N-1๊ฐœ์˜ ์›ํŒ์„ ๋จผ์ € ๋ชฉ์ ์ง€๊ฐ€ ์•„๋‹Œ ๊ณณ์œผ๋กœ ์˜ฎ๊ธด๋‹ค.(N-1๊ฐœ๋ฅผ from -> other)

 

move(from, to)

 -> N-1๊ฐœ์˜ ์›ํŒ์„ ์˜ฎ๊ฒผ์œผ๋ฉด, ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์›ํŒ์ด 1๊ฐœ ๋‚จ์œผ๋ฏ€๋กœ, ๊ทธ ์›ํŒ์„ ๋ชฉ์ ์ง€๋กœ ์˜ฎ๊ธด๋‹ค.

 

hanoi(N-1, other, from, to)

 -> ์ด์ œ ๋ชฉ์ ์ง€๊ฐ€ ์•„๋‹Œ ๊ณณ์— ์กด์žฌํ•˜๋Š” N-1๊ฐœ์˜ ์›ํŒ์„ ๋ชฉ์ ์ง€๋กœ ์˜ฎ๊ธด๋‹ค.(N-1๊ฐœ๋ฅผ other -> to)

 

 

https://brenden.tistory.com/31

 

 

https://shoark7.github.io/programming/algorithm/tower-of-hanoi

 

ํŒฉํ† ๋ฆฌ์–ผ ํ•จ์ˆ˜๋ฅผ ๋ณด์ž.

5! = 5*4*3*2*1 ์ด๋‹ค.

์ฆ‰ 5! = 5 * 4! ๊ณผ ๋™์ผํ•˜๋‹ค.

์ด ๋ง์€, n! = n * factorial(n-1) ๊ณผ ๋™์ผํ•œ๋ฐ, 4!์„ ์žฌ๊ท€๋ฅผ ํ†ตํ•ด factorial(n-1)๋กœ ํ˜ธ์ถœํ•œ๋‹ค.

public class Main {
	
	public static int factorial(int n) {
		if(n == 1)
			return 1;
		else {
			return n * factorial(n-1);
		}
	}

	public static void main(String[] args) {
		int n = 5;
		int result = factorial(n);
		System.out.println(result);
	}
}

https://gomguard.tistory.com/111

 

 

 

 

1 ~ n๊นŒ์ง€์˜ ํ•ฉ์„ ๋ณด์ž.

1 ~ n๊นŒ์ง€์˜ ํ•ฉ์€ n + (1 ~ n-1๊นŒ์ง€์˜ ํ•ฉ) ๊ณผ ๋™์ผํ•˜๋‹ค.

์ฆ‰, 1 ~ n-1๊นŒ์ง€์˜ ํ•ฉ์„ ์žฌ๊ท€๋ฅผ ํ†ตํ•ด factorial(n-1)์„ ํ˜ธ์ถœํ•œ๋‹ค.

public class Main {
	
	public static int factorial(int n) {
		if(n == 1)
			return 1;
		else {
			return n + factorial(n-1);
		}
	}

	public static void main(String[] args) {
		int n = 10;
		int result = factorial(n);
		System.out.println(result);
	}
}

 

 

โ— ๊ธฐํƒ€ ์ถ”๊ฐ€ ์„ค๋ช…

 

ํ•˜๋…ธ์ด์˜ํƒ‘ ๋ฌธ์ œ์˜ ์ดํ•ด๋ฅผ ์œ„ํ•œ ๋ณด์ถฉ์„ค๋ช…

https://www.codeit.kr/community/threads/7681

 

ํ•˜๋…ธ์ดํƒ‘ ํผ์ฆ์„ ์‰ฝ๊ฒŒ ํ‘ธ๋Š” ํ•ด๋ฒ•

https://puzzlemuseum.tistory.com/443

 

 

 

โ˜…โ˜…โ˜…ํ•˜๋…ธ์ด์˜ ํƒ‘ ์ดํ•ดํ•˜๊ธฐ

https://shoark7.github.io/programming/algorithm/tower-of-hanoi

 

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€