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();
}
}
ํ์ด
์ฌ๊ท ํจ์์ ๋ํ ๋ฌธ์ ๋ก ์ ๋ช ํ ํ๋ ธ์ด์ ํ๋ฌธ์ ๋ฅผ ํ์ด๋ณด์๋ค.
์ฌ๊ทํจ์๊ฐ ๋ญ๊ฐ์? ๋ผ๋ ์ข์ ์ฌ๊ท ๋ฌธ์ ๋ ์๋ค.
์ผ๋จ ํ๋ ธ์ด ํ ๋ฌธ์ ์ ์กฐ๊ฑด์ ๋ค์๊ณผ ๊ฐ๋ค.
1. ํ ๋ฒ์ ํ ๊ฐ์ ์ํ๋ง์ ๋ค๋ฅธ ํ์ผ๋ก ์ฎ๊ธธ ์ ์๋ค.
2. ์์ ๋์ ์ํ์ ํญ์ ์์ ๊ฒ์ด ์๋์ ๊ฒ๋ณด๋ค ์์์ผ ํ๋ค.
ํ๋ ธ์ด ํ ๋ฌธ์ ์ ์๋ฆฌ๋ ๋ค์๊ณผ ๊ฐ๋ค.
์ ์ฌ์ง๊ณผ ๊ฐ์ด 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)
ํฉํ ๋ฆฌ์ผ ํจ์๋ฅผ ๋ณด์.
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);
}
}
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
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Codeforces] 1311A: Add Odd or Subtract Even (0) | 2020.03.12 |
---|---|
[Codeforces] 1003A: Polycarp's Pockets (0) | 2020.03.11 |
[Codeforces] 978B: File Name (0) | 2020.03.11 |
[๋ฐฑ์ค] 17478๋ฒ: ์ฌ๊ทํจ์๊ฐ ๋ญ๊ฐ์?(์ฌ๊ท) (0) | 2020.03.10 |
[Codeforces] 1207A: There Are Two Types Of Burgers (0) | 2020.03.10 |
๋๊ธ