Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package testy.parmutace;
- import java.util.ArrayList;
- import java.util.Deque;
- import java.util.LinkedList;
- import java.util.List;
- /**
- *
- * @author osiris
- */
- public class TestyParmutace {
- /**
- * @param args the command line arguments
- *
- */
- public static final ArrayList<Deque<Integer>> finalList = new ArrayList<Deque<Integer>>();;
- public static void main(String[] args) {
- // TODO code application logic here
- List<Place> places = new ArrayList<Place>();
- Place place1 = new Place();
- place1.tokens.add(1);
- place1.tokens.add(2);
- place1.tokens.add(3);
- places.add(place1); //add place to the list
- Place place2 = new Place();
- place2.tokens.add(3);
- place2.tokens.add(4);
- place2.tokens.add(5);
- place2.tokens.add(10);
- places.add(place2); //add place to the list
- Place place3 = new Place();
- place3.tokens.add(6);
- place3.tokens.add(7);
- place3.tokens.add(8);
- places.add(place3); //add place to the list
- //
- //so we have
- //P1 = {1,2,3}
- //P2 = {3,4,5}
- //P3 = {6,7,8}
- Deque<Integer> tokens = new LinkedList<Integer>();
- Func(places,0,tokens);
- System.out.println("finallist:"+ finalList.get(0));
- }
- /**
- *
- * @param places list of places
- * @param index index of current place
- * @param tokens list of tokens
- * @return true if we passed guard, false if we did not
- */
- public static void Func( List<Place> places, int index, Deque<Integer> tokens) {
- if (index == places.size()) {
- // if control reaches here, it means that we've recursed through a particular combination
- // ( consisting of exactly 1 token from each place ), and there are no more "places" left
- String outputTokens = "";
- for (int token : tokens) {
- outputTokens += token+",";
- }
- finalList.add(tokens);
- } else {
- Place p = places.get(index);
- for (Integer token : p.tokens) {
- tokens.add(token);
- Func(places, index+1, tokens);
- tokens.removeLast();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement