0

N個のリストからのすべての順列が必要です。プログラムが開始されるまでNはわかりません。これが私のSSCCEです(私にアドバイスされたアルゴリズムを実装しましたが、いくつかのバグがあります)。

まず、Place クラスを作成します。

public class Place {
public List<Integer> tokens ;

//constructor
public Place() {

  this.tokens = new ArrayList<Integer>();  
}

}

そして、クラスをテストします:

public class TestyParmutace {

    /**
     * @param args the command line arguments
     */
    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);
        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}


        List<Integer> tokens = new ArrayList<Integer>();

        Func(places,0,tokens);

    }

    /**
     * 
     * @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 boolean Func( List<Place> places, int index, List<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 i = 0; i< tokens.size(); i++) {

                    outputTokens+= tokens.get(i) +",";
                }
                System.out.println("Tokens: "+outputTokens);

                if (tokens.get(0) == 4 && tokens.get(1) == 5 && tokens.get(2) == 10) {
                    System.out.println("we passed the guard with 3,5,8");
                    return true;
                }

                else {
                    tokens.remove(tokens.get(tokens.size()-1));
                    return false;
                }

            }

        Place p = places.get(index);

        for (int i = 0; i< p.tokens.size(); i++)
            {

                tokens.add(p.tokens.get(i));
                //System.out.println("Pridali sme token:" + p.tokens.get(i));

                if ( Func( places, index+1, tokens ) ) return true;

            }
        if (tokens.size()>0)
            tokens.remove(tokens.get(0));

        return false;

    }
}

このコードの出力は次のとおりです。

Tokens: 1,3,6,
Tokens: 1,3,7,
Tokens: 1,3,8,
Tokens: 3,4,6,
Tokens: 3,4,7,
Tokens: 3,4,8,
Tokens: 4,5,6,
Tokens: 4,5,7,
Tokens: 4,5,8,
Tokens: 2,3,6,
Tokens: 2,3,7,
Tokens: 2,3,8,
Tokens: 3,4,6,
Tokens: 3,4,7,
Tokens: 3,4,8,
Tokens: 4,5,6,
Tokens: 4,5,7,
Tokens: 4,5,8,
Tokens: 3,3,6,
Tokens: 3,3,7,
Tokens: 3,3,8,
Tokens: 3,4,6,
Tokens: 3,4,7,
Tokens: 3,4,8,
Tokens: 4,5,6,
Tokens: 4,5,7,
Tokens: 4,5,8,

したがって、いくつかの組み合わせは正しい (1,3,6)、いくつかは正しくない (4,5,8)、いくつかは完全に欠落しています (2,4,8,..) この問題を解決するにはどうすればよいでしょうか? 場所の数と場所のトークンの数は異なる場合があります.2つの場所では機能するため、3つの場所を使用しましたが、より多くの場所ではバグがあります. ありがとう。

4

2 に答える 2

4

あなたのアルゴリズムはほぼ正しいです。を取得したときに、現在の反復に戻っtrueたり停止したりする必要はないと思います。私はあなたの方法を変更しました:falsetrueFunc

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 + ",";
        }
        System.out.println("Tokens: "+outputTokens);
    } else {
        Place p = places.get(index);
        for (int token : p.tokens) {
            tokens.addLast(token);
            Func(places, index+1, tokens);
            token.removeLast();
        }
    }
}

最後に追加されたトークンを削除するための便利な方法を提供するため、Dequeを使用しました。の実装としてremoveLast渡すことができます。LinkedListDeque

アップデート

List<List<Integer>> combinations;

// Instead of printing result:
List<Integer> copy = new ArrayList<Integer>(tokens);
combinations.add(copy);
于 2012-04-08T11:35:24.633 に答える
0

あなたは実際には順列ではなく、外積を設定しようとしています。だからあなたはする必要があります

 for(Integer token1 : place1.tokens){
    for(Integer token2 : place2.tokens){
       for(Integer token3 : place3.tokens){
            //crossValue = (token1, token2, token3);
        }
    }
 }

ここで、すべての順列が必要な場合、たとえば、、など[1,3,6,]も必要な場合は、リストまたは配列を指定して順列を出力する関数が必要です。配列の 順列を参照してください。[1,6,3][3,6,1]

于 2012-04-08T11:33:34.373 に答える