Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package biber;
  7.  
  8. public class BiberSilo {
  9.    
  10.     private java.util.ArrayList<Biber> nodeList;
  11.    
  12.     public BiberSilo(int n)  // Finde Leerlauflänge (LLL) für alle
  13.     {                        // Konstellationen mit Gesamtzahl Biber n
  14.         int maxLLL = 0;
  15.         int maxA = 0, maxB = 0, maxC = 0;
  16.        
  17.         // Durchlaufe alle Konstellationen mit Bibermenge n.
  18.         // i=Startzahl Silo A, j=Startzahl Silo B
  19.         for(int i=1;i<n/2;i++)
  20.         {
  21.             for(int j=1;j<(n-i)/2;j++)
  22.             {
  23.                 // Berechne LLL für aktuelle Konstellation
  24.                 int actLLL = BiberSolver(new Biber(i,j,(n-i-j),0));
  25.                
  26.                 // Speichere Konstellation falls neues
  27.                 // maximales LLL gefunden
  28.                 if(actLLL>maxLLL)
  29.                 {
  30.                     maxLLL = actLLL;
  31.                     maxA = i;
  32.                     maxB = j;
  33.                     maxC = (n-i-j);
  34.                 }
  35.             }
  36.         }
  37.        
  38.         System.out.println("Schwierigste Konstellation ist ("+maxA+","+maxB+","+maxC+") mit einem LLL="+maxLLL);
  39.     }
  40.    
  41.    
  42.    
  43.     public int BiberSolver(Biber Biber)
  44.     {
  45.         // Lege neue Listenstruktur an
  46.         nodeList = new java.util.ArrayList<Biber>();
  47.         // Füge die Startkonstellation hinzu
  48.         nodeList.add(Biber);
  49.        
  50.         while(true)
  51.         {
  52.             // Entferne vordersten Knoten
  53.             Biber actNode = nodeList.get(0);
  54.             nodeList.remove(0);
  55.            
  56.             // Aktuelle Konstellation ist Lösung?
  57.             if(actNode.isSolution())
  58.                 return actNode.n+1;
  59.            
  60.             // Führe die drei möglichen Züge durch und füge die
  61.             // dadurch entstehenden Konstellationen der Liste hinzu
  62.             nodeList.add(new Biber(actNode.a-actNode.b,actNode.b*2,actNode.c,actNode.n+1));
  63.             nodeList.add(new Biber(actNode.a-actNode.c,actNode.b,actNode.c*2,actNode.n+1));
  64.             nodeList.add(new Biber(actNode.a,actNode.b-actNode.c,actNode.c*2,actNode.n+1));
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement