Implementasi Hanoi Of Tower di Java Programming

  1. /*
  2.  * Java Program to Solve Tower of Hanoi Problem using Stacks
  3.  */
  4. 
    
  5.  import java.util.*;
  6. 
    
  7.  /* Class TowerOfHanoiUsingStacks */
  8.  public class TowerOfHanoiUsingStacks
  9.  {
  10.      public static int N;
  11.      /* Creating Stack array  */
  12.      public static Stack<Integer>[] tower = new Stack[4];
  13. 
    
  14.      public static void main(String[] args)
  15.      {
  16.          Scanner scan = new Scanner(System.in);
  17.          tower[1] = new Stack<Integer>();
  18.          tower[2] = new Stack<Integer>();
  19.          tower[3] = new Stack<Integer>();
  20.          /* Accepting number of disks */
  21.          System.out.println("Enter number of disks");
  22.          int num = scan.nextInt();
  23.          N = num;
  24.          toh(num);
  25.      }
  26.      /* Function to push disks into stack */
  27.      public static void toh(int n)
  28.      {
  29.          for (int d = n; d > 0; d--)
  30.              tower[1].push(d);
  31.          display();
  32.          move(n, 1, 2, 3);
  33.      }
  34.      /* Recursive Function to move disks */
  35.      public static void move(int n, int a, int b, int c)
  36.      {
  37.          if (n > 0)
  38.          {
  39.              move(n-1, a, c, b);
  40.              int d = tower[a].pop();
  41.              tower[c].push(d);
  42.              display();
  43.              move(n-1, b, a, c);
  44.          }
  45.      }
  46.      /*  Function to display */
  47.      public static void display()
  48.      {
  49.          System.out.println("  A  |  B  |  C");
  50.          System.out.println("---------------");
  51.          for(int i = N - 1; i >= 0; i--)
  52.          {
  53.              String d1 = " ", d2 = " ", d3 = " ";
  54.              try
  55.              {
  56.                  d1 = String.valueOf(tower[1].get(i));
  57.              }
  58.              catch (Exception e){
  59.              }
  60.              try
  61.              {
  62.                  d2 = String.valueOf(tower[2].get(i));
  63.              }
  64.              catch(Exception e){
  65.              }
  66.              try
  67.              {
  68.                  d3 = String.valueOf(tower[3].get(i));
  69.              }
  70.              catch (Exception e){
  71.              }
  72.              System.out.println("  "+d1+"  |  "+d2+"  |  "+d3);
  73.          }
  74.          System.out.println("\n");
  75.      }
  76.  }

 

sumber : sanfoundry

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s