JAVA 프로그래밍

문제

하노이 탑에서 출발기둥에 있는 여러 원반을 도착기둥으로 모두 옮기는 문제입니다 이를 해결하는 프로그램의 다음 실행상태에 대해 빈칸을 채우세요 

프로그램의 실행순서 및 실행상태

1  public static void main( String[] args ) {  

하노이 탑의 원반 개수를 입력하세요(10개 이하) : 2

2   initializeTower( diskCount );

F1b  private static void initializeTower( int diskCount ) {

F1e  }


1 . .
2 . .
--- --- ---
A B C

3   moveDisks( diskCount, 0, 1, 2 );

F3b  private static void moveDisks( int diskCount, int from, int temp, int to ) {  

F32    moveDisks( diskCount-1, from, to, temp );

F3b  private static void moveDisks( int diskCount, int from, int temp, int to ) {  

F31    moveOneDisk( from, to );

F2b  private static void moveOneDisk( int from, int to ) {  


. . .
2 1 .
--- --- ---
A B C

F2e  }

F3e  }
                       [0]           [1]           [2]     
         tower[1] 1  0   0 
              [0]  2  0  0 
diskCountPerTower 2 0  0 
   totalDiskCount  2 
   main()
diskCount  2 
  ()
diskCount  from   temp    to  
    2       0     1     2  
  ()
diskCount  from   temp    to  

F33    moveOneDisk( from, to );

F2b  private static void moveOneDisk( int from, int to ) {  


. . .
. 1 2
--- --- ---
A B C

F2e  }

F34    moveDisks( diskCount-1, temp, from, to );

F3b  private static void moveDisks( int diskCount, int from, int temp, int to ) {  

F31    moveOneDisk( from, to );

F2b  private static void moveOneDisk( int from, int to ) {  


. . 1
. . 2
--- --- ---
A B C

F2e  }

F3e  }
                       [0]           [1]           [2]     
         tower[1]  0   0  0
              [0]  0  1  2 
diskCountPerTower  0  1 1
   totalDiskCount  2 
   main()
diskCount  2 
  ()
diskCount  from   temp    to  
    2       0     1     2  
  ()
diskCount  from   temp    to  

F3e  }

4  }


프로그램 코드

	import java.util.Scanner;
	
	public class TowerOfHanoi
	{		
		private static int[][] tower = new int[3][10];
		private static int[] diskCountPerTower = new int[3];
		private static int totalDiskCount;
		
F1b		private static void initializeTower( int diskCount ) {
			totalDiskCount = diskCount;
			diskCountPerTower[0] = diskCount;
			diskCountPerTower[1] = 0;
			diskCountPerTower[2] = 0;
			for ( int i = 0; i < diskCount; i++ ) {
				tower[0][i] = diskCount - i;
			}
F1e		}
		
		private static void printTower() {
			for ( int line = totalDiskCount-1; line >= 0; line-- ) {
				for ( int number = 0; number < 3; number++ ) {
					if ( tower[number][line] > 0 ) 
						System.out.print( "  " + tower[number][line] + "  " );
					else 
						System.out.print( "  .  " );
				}
				System.out.println();
			}
			System.out.println(" ---  ---  ---\n  A    B    C \n");	
		}
		
F2b		private static void moveOneDisk( int from, int to ) {		
			int top = --diskCountPerTower[from];
			int disk = tower[from][top];
			tower[from][top] = 0;
			
			top = diskCountPerTower[to]++;
			tower[to][top] = disk;
				
			printTower();
F2e		}
		
F3b		private static void moveDisks( int diskCount, int from, int temp, int to ) {		
			if ( diskCount == 1 ) {
F31				moveOneDisk( from, to );
			}
			else {
F32				moveDisks( diskCount-1, from, to, temp );
F33				moveOneDisk( from, to );
F34				moveDisks( diskCount-1, temp, from, to );
			}
F3e		}
1		public static void main( String[] args ) { 	
			Scanner scan = new Scanner( System.in );
			System.out.print( "하노이 탑의 원반 개수를 입력하세요(10개 이하) : " );
			int diskCount = scan.nextInt();
2			initializeTower( diskCount );
			printTower();
		
3			moveDisks( diskCount, 0, 1, 2 );
			scan.close();
4		}
	}