Advertisement
Guest User

Untitled

a guest
Apr 10th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package testy.parmutace;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.Deque;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11.  
  12. /**
  13.  *
  14.  * @author osiris
  15.  */
  16. public class TestyParmutace {
  17.  
  18.     /**
  19.      * @param args the command line arguments
  20.      *
  21.      */
  22.    
  23.    public static final ArrayList<Deque<Integer>> finalList = new ArrayList<Deque<Integer>>();;
  24.     public static void main(String[] args) {
  25.         // TODO code application logic here
  26.        
  27.         List<Place> places = new ArrayList<Place>();
  28.        
  29.         Place place1 = new Place();
  30.         place1.tokens.add(1);
  31.         place1.tokens.add(2);
  32.         place1.tokens.add(3);
  33.         places.add(place1); //add place to the list
  34.        
  35.         Place place2 = new Place();
  36.         place2.tokens.add(3);
  37.         place2.tokens.add(4);
  38.         place2.tokens.add(5);
  39.         place2.tokens.add(10);
  40.         places.add(place2); //add place to the list
  41.        
  42.         Place place3 = new Place();
  43.         place3.tokens.add(6);
  44.         place3.tokens.add(7);
  45.         place3.tokens.add(8);
  46.         places.add(place3); //add place to the list
  47. //        
  48.         //so we have
  49.         //P1 = {1,2,3}
  50.         //P2 = {3,4,5}
  51.         //P3 = {6,7,8}
  52.        
  53.        
  54.         Deque<Integer> tokens = new LinkedList<Integer>();
  55.  
  56.         Func(places,0,tokens);
  57.         System.out.println("finallist:"+ finalList.get(0));
  58.  
  59.  
  60.        
  61.     }
  62.    
  63.     /**
  64.      *
  65.      * @param places list of places
  66.      * @param index index of current place
  67.      * @param tokens list of tokens
  68.      * @return true if we passed guard, false if we did not
  69.      */
  70. public static void Func( List<Place> places, int index, Deque<Integer> tokens) {
  71.    
  72.    
  73.     if (index == places.size()) {
  74.         // if control reaches here, it means that we've recursed through a particular combination
  75.         // ( consisting of exactly 1 token from each place ), and there are no more "places" left
  76.         String outputTokens = "";
  77.         for (int token : tokens) {
  78.             outputTokens += token+",";
  79.         }
  80.        
  81.        finalList.add(tokens);
  82.        
  83.     } else {
  84.         Place p = places.get(index);
  85.         for (Integer token : p.tokens) {
  86.             tokens.add(token);
  87.             Func(places, index+1, tokens);
  88.             tokens.removeLast();
  89.         }
  90.        
  91.     }
  92.    
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement