RSS
Facebook
Twitter

Thursday, February 17, 2011

penjelesan dari program hanoi

import javax.swing.JOptionPane;
    public class hanoi2
    {
        static int i=0;
        public static int Hanoi(int N,char home, char help, char destination)
        {
            if (N==1)
            {
                i++;
                System.out.println("Move "+i+" plate "+N+" from "+home+" to "+destination);
                return 0;
            }
            Hanoi(N-1, home, destination, help);
            i++;
            System.out.println("Move "+i+ " Plste "+N+" from "+home+" to "+destination);
            Hanoi(N-1, help, home, destination);
            return 0;
        }
        public static void main(String[] args)
        {
            int N;
            String input=JOptionPane.showInputDialog("Input the plate :");
            N=Integer.parseInt(input);
            Hanoi(N,'A','B','C');
        }
    }
For INPUT=2
Hanoi(N,'A','B','C'); -- >  Hanoi(2,'A','B','C');
public static int Hanoi(int N=2,char home=A, char help=B, char destination=C)
            if (N==1)  ---- >  N==1??? no
            Hanoi(N-1, home, destination, help); ---- > call the function
public static int Hanoi(int N=1,char home=home (A), char help=destination (C), char destination=help (B))
if (N==1) --- >  N==1??? yes
                    {
                    i++; ----> i=1
System.out.println("Move "+i+" Plate "+N+" from "+home+" to "+destination); --- > i=1 , N=1 , home = A ,destination = B
                        return 0;
                    }
i++; --- > i=2
            System.out.println("Move "+i+ " Plate "+N+" from "+home+" to "+destination); -- > i=2 , N=2 , home=A, destination = C
            Hanoi(N-1, help, home, destination); --- > call the function
public static int Hanoi(int N=1,char home=help (B), char help=home (A), char destination=destination (C))
if (N==1) --- >  N==1??? yes
                    {
                    i++; ----> i=3
System.out.println("move "+i+" Plate "+N+" from "+home+" to "+destination); --- > i=3 , N=1 , home = B ,destination = C
                        return 0;
                    }




   

For INPUT=3
Hanoi(N,'A','B','C'); -- >  Hanoi(3,'A','B','C');
public static int Hanoi(int N=3,char home=A, char help=B, char destination=C)
            if (N==1)  ---- >  N==1??? No
            Hanoi(N-1, home, destination, help); ---- > call the function
public static int Hanoi(int N=2,char home=home (A), char help=destination (C), char destination=help (B))
if (N==1) --- >  N==1??? no
                   Hanoi(N-1, home, destination, help); ---- > call the function
public static int Hanoi(int N=1,char home=home (A), char help=destination (B), char destination=help (C))
if (N==1) --- >  N==1??? Yes
                    {
                    i++; ----> i=1
System.out.println("Move "+i+" Plate "+N+" from "+home+" to "+destination); --- > i=1 , N=1 , home = A , destination = C
                        return 0;
                    }

i++; --- > i=2
System.out.println("Move "+i+ " Plate "+N+" from "+home+" to "+destination); -- > i=2 , N=2 , home = A, destination = B
Hanoi(N-1, help, home, destination); --- > call the function
public static int Hanoi(int N=1,char home=help (C), char help=home (A), char destination=destination (B))
if (N==1) --- >  N==1??? Yes
                    {
                    i++; ----> i=3
System.out.println("Move "+i+" Plate "+N+" from "+home+" to "+destination); --- > i=3 , N=1 ,home = C , destination = B
                        return 0;
                    }
            i++; -- > i=4
            System.out.println("Move "+i+ " Plate "+N+" from "+home+" to "+destination); -- > i=4 , N = 3 , home= A , destination = C
            Hanoi(N-1, help, home, destination); -- > call the function
public static int Hanoi(int N=2,char home=help (B), char help=home (A), char destination=destination (C))
if (N==1) --- >  N==1??? no
                   Hanoi(N-1, home, destination, help); ---- > call the function
public static int Hanoi(int N=1,char home=home (B), char help=destination (C), char destination=help (A))
if (N==1) --- >  N==1??? Yes
                    {
                    i++; ----> i=5
System.out.println("Move "+i+" Plate "+N+" from "+home+" to "+destination); --- > i=5 , N=1 , home = B , destination = A
                        return 0;
                    }

i++; --- > i=6
System.out.println("Move "+i+ " Plate "+N+" from "+home+" to "+destination); -- > i=6 , N=2 , home = B, destination = C
Hanoi(N-1, help, home, destination); --- > call the function
public static int Hanoi(int N=1,char home=help (A), char help=home (B), char destination=destination (C))
if (N==1) --- >  N==1??? Yes
                    {
                    i++; ----> i=7
System.out.println("Move "+i+" Plate "+N+" from "+home+" to "+destination); --- > i=7 , N=1 ,home = A , destination = C
                        return 0;
                    }
            return 0;
        }

0 komentar: