language: Java (sun-jdk-1.7.0_10)
date: 872 days 0 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 class HanoiNK{
    
        public static void main(String args[]){
        
                        int n = 4;
                        int k = 5;
                        
                        try{
                                slide(k, n, 'A', 'B', 'C');
                        }catch(Exception e){
                                System.out.println(e);
                        }
        }
        
        public static void slide(int counter, int hoehe, char quelle,
                                  char ablage, char ziel) throws Exception{             
                if(counter > 0){
                        if(hoehe == 1){                                                         
                                System.out.println("move "+ hoehe +" from " +
                                                    quelle + " to " + ziel);
                        }else{  
                                counter--;
                                slide(counter, hoehe - 1, quelle, ziel, ablage);        
                                System.out.println("move "+ hoehe +" from " +
                                                    quelle + " to " + ziel);
                                counter--;
                                slide(counter, hoehe - 1, ablage, quelle, ziel);        
                        }
                }else{
                        throw new Exception("stop");
                }
        }
    }
  • upload with new input
  • result: Success     time: 0.05s    memory: 213312 kB     returned value: 0

    move 1 from A to B
    move 2 from A to C
    move 1 from B to C
    move 3 from A to B
    move 1 from C to A
    move 2 from C to B
    java.lang.Exception: stop