Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by Dude ( 15 years ago )
/*
Aufgabe 20. 
Ändern Sie class TowersOfHanoi der Vorlesung ab zu class TowersOfHanoiPlus, 
so dass bei jedem Schritt des Lösungsverfahrens nicht die Scheibenbewegung "i->j" protokolliert wird, 
sondern jeweils der resultierende Zustand. Verwenden Sie dafür eine Druckzeile, 
die die Stangenbelegungen wiedergibt durch drei "Vektoren" 
(= erste, zweite, dritte Stange, unten = erste Position im Vektor) 
und mit n Elementen aus "1","2", .., "n" und "-" 
(Zahlen = Scheiben entsprechender Grösse, "-" = keine Scheibe).

Beispiel: hier die drei ersten Zustände bei Aufruf mit n = 4 
{4,3,2,1}{-,-,-,-}{-,-,-,-}
{4,3,2,-}{1,-,-,-}{-,-,-,-}
{4,3,-,-}{1,-,-,-}{2,-,-,-}

Hinweis: Halten Sie sich genau an das hier illustrierte Ausgabeformat! 
Sie müssen die move-Methode ändern, 
und Sie brauchen eine Instanzvariable zur internen Darstellung des aktuellen Zustands. 
Schreiben Sie auch ein Demoprogramm für Ihre Klasse.
*/

class TowersOfHanoiPlus{
private int nrOfDisks;           //defines problem instance

 private String headline; 
 private String comment;
 private int counter = 0;

 private int[] array1;
 private int[] array2;
 private int[] array3;
 
 private int[] temp = new int[4];

   TowersOfHanoiPlus(int n) {           //constructor
                                    //requires n > 0
  nrOfDisks = n;
  
  array1 = new int[n]; // Stange 1
  array2 = new int[n]; // Stange 2
  array3 = new int[n]; // Stange 3
  
   for(int i = 0; i < array1.length; i++)
    array1[i] = array1.length - i;
   for(int i = 0; i < array2.length; i++)
    array2[i] = 0;
   for(int i = 0; i < array2.length; i++)
    array2[i] = 0;
   
   temp[1] = n;   
       // Bestimmung der Höchsten Stellen der arrays
  
  headline = "TowersOfHanoiPlus: " + nrOfDisks + " disks";
   }

   public void solveIt() {           //interface solution method
      System.out.println(headline);
      System.out.println();
      solveIt(nrOfDisks, 1,3,2);
   }

   private void solveIt              //internal solution method 
      (int n, int source, int target, int helper) {
  
      if (n == 1)
         move(source, target);
      else 
         {
         solveIt(n-1,source,helper,target);
         move(source, target);
         solveIt(n-1,helper,target,source);
         }
   }

   private void move                //describe solution process
   (int from, int to) {
  
  if(counter < 1){    // Erste Zeile Ausdrucken lasses
   System.out.print("{");
   for(int i = 0; i < array1.length; i++){
    if(array1[i] == 0)
     System.out.print("-"); 
    else
     System.out.print(array1[i]);
    if(array1.length - 1 > i)
     System.out.print(",");
   }
   System.out.print("}{");
   for(int i = 0; i < array2.length; i++){
    if(array2[i] == 0)
     System.out.print("-"); 
    else
     System.out.print(array2[i]);
    if(array2.length - 1 > i)
     System.out.print(",");
   }
   System.out.print("}{");
   for(int i = 0; i < array3.length; i++){
    if(array3[i] == 0)
     System.out.print("-"); 
    else
     System.out.print(array3[i]);
    if(array3.length - 1 > i)
     System.out.print(",");
   }
   System.out.print("}n");
  counter++;
  }
  
  if(from == 1){
   if(to == 2){
     array2[temp[to]] = array1[temp[from]-1];
     array1[temp[from]-1] = 0;
     temp[from] -= 1;
     temp[to] += 1;
   }else if(to == 3){
     array3[temp[to]] = array1[temp[from]-1];
     array1[temp[from]-1] = 0;
     temp[from] -= 1;
     temp[to] += 1;
   }
  }else if(from == 2){
   if(to == 1){
     array1[temp[to]] = array2[temp[from]-1];
     array2[temp[from]-1] = 0;
     temp[from] -= 1;
     temp[to] += 1;
   }else if(to == 3){
     array3[temp[to]] = array2[temp[from]-1];
     array2[temp[from]-1] = 0;
     temp[from] -= 1;
     temp[to] += 1;
   }
  }else{
   if(to == 1){
     array1[temp[to]] = array3[temp[from]-1];
     array3[temp[from]-1] = 0;
     temp[from] -= 1;
     temp[to] += 1;
   }else if(to == 2){
     array2[temp[to]] = array3[temp[from]-1];
     array3[temp[from]-1] = 0;
     temp[from] -= 1;
     temp[to] += 1;
   }
  }
  
  System.out.print("{");   // Ausgabe
  for(int i = 0; i < array1.length; i++){
   if(array1[i] == 0)
    System.out.print("-"); 
   else
    System.out.print(array1[i]);
   if(array1.length - 1 > i)
    System.out.print(",");
  }
  System.out.print("}{");
  for(int i = 0; i < array2.length; i++){
   if(array2[i] == 0)
    System.out.print("-"); 
   else
    System.out.print(array2[i]);
   if(array2.length - 1 > i)
    System.out.print(",");
  }
  System.out.print("}{");
  for(int i = 0; i < array3.length; i++){
   if(array3[i] == 0)
    System.out.print("-"); 
   else
    System.out.print(array3[i]);
   if(array3.length - 1 > i)
    System.out.print(",");
  }
  System.out.print("}n");  
   }
 }

 

Revise this Paste

Your Name: Code Language: