challenge 総当たり戦の日程作成

任意の偶数Nのチームの総当たり戦を最短日数(N-1日)で行う場合の日程表を1つ作成してください。

解はひとつではない場合もあります。
もし、余力があれば、全ての可能性も求めてください。

これは、スポーツスケジューリングと言う分野の問題で、数学的には、カークマンの問題と言うのが近いようです。

例えば、4チームであれば、

1-2 3-4
1-3 2-4
1-4 2-3

6チームであれば

1-2 3-4 5-6 
1-3 2-5 4-6 
1-4 2-6 3-5 
1-5 2-4 3-6 
1-6 2-3 4-5

が解のひとつです。

Posted feedbacks - Java

強引に全ての解を求めるプログラムです。チームを区別しないで同じになる解は排除するようになっています。引数にチーム数(偶数)を与えて来どうしてください(奇数を与えても総当たり戦の日程表は求められません)。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class Sample {
    boolean[][] match;
    int[][] pair;
    int num;
    public Sample(int num) {
        match = new boolean[num][num];
        pair = new int[num - 1][num/2];
        this.num = num;
    }
    public void work(int count) {
        if (count < num - 1) {
            boolean[] done = new boolean[num];
            work1(count, done, 0, 0);
        } else {
            for (int i = 0; i < num - 1; i++) {
                for (int j = 0; j < num / 2; j++) {
                    System.out.printf("%d-%d ", (pair[i][j] >> 8) + 1, (pair[i][j] & 255) + 1);
                }
                System.out.println();
            }
            System.out.println();
        }
    }
    public void work1(int count, boolean[] done, int coat, int origin) {
        if (coat < num / 2) {
            for (int i = origin; i < num; i++) {
                if (!done[i]) {
                    for (int j = i + 1; j < num; j++) {
                        if (!done[j] && !match[i][j]) {
                            done[i] = done[j] = match[i][j] = true;
                            pair[count][coat] = (i << 8) + j;
                            work1(count, done, coat + 1, i + 1);
                            done[i] = done[j] = match[i][j] = false;
                        }
                    }
                }
            }
        } else {
            work(count + 1);
        }
    }
    public static void main(String[] args) {
        Sample s = new Sample(Integer.parseInt(args[0]));
        s.work(0);
    }
}

Index

Feed

Other

Link

Pathtraq

loading...