Skip to content

Commit

Permalink
Change
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyi-kim committed Mar 25, 2024
1 parent a4831e2 commit cbaa4da
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions chapter06/problem_b/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
public class Main {
public static final Scanner scanner = new Scanner(System.in);



/**
* @brief 시작 기둥(from)으로부터 목적지 기둥(to)까지 num개의 원반을 모두 옮기는 최소 이동 횟수를 계산하는 함수
*
Expand All @@ -14,14 +16,15 @@ public class Main {
* @param to 원반들을 모두 옮길 기둥
* @return 모든 원반을 옮길 수 있는 최소 이동 횟수
*/
public static int getMinimumMove(int num, Stack<Disk> from, Stack<Disk> buffer, Stack<Disk> to) {
public static int getMinimumMove(int num, Bar from, Bar buffer, Bar to) {
if (num == 0) {
// 원판이 없다면 당연히 아무 이동도 필요하지 않다
return 0;
}else if(num == 1){
// 원판이 하나 뿐이라면 그냥 이동하면 된다
Disk d = from.pop();
to.push(d);
Disk d = from.stack.pop();
to.stack.push(d);
// System.out.println(from.name + " -> " + to.name);
return 1;
}

Expand All @@ -39,17 +42,19 @@ public static int getMinimumMove(int num, Stack<Disk> from, Stack<Disk> buffer,
return move;
}



public static void main(String[] args) throws Exception {
int N = scanner.nextInt();

Stack<Disk> A = new Stack<>();
Stack<Disk> B = new Stack<>();
Stack<Disk> C = new Stack<>();
Bar A = new Bar("A");
Bar B = new Bar("B");
Bar C = new Bar("C");

// 첫 번째 기둥에 N개의 원판을 쌓는다
for (int i = N; i >= 1; i -= 1) {
Disk d = new Disk(i);
A.push(d);
A.stack.push(d);
}

int answer = getMinimumMove(N, A, B, C);
Expand All @@ -58,6 +63,14 @@ public static void main(String[] args) throws Exception {
}
}

class Bar{
String name;
Stack<Disk> stack;
Bar(String name){
this.name = name;
this.stack = new Stack<>();
}
}

class Disk {
final int size;
Expand Down

0 comments on commit cbaa4da

Please sign in to comment.