総当たり戦の日程作成
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);
}
}
|


ryugate
#5661()
Rating2/2=1.00
see: カークマンの組分け
[ reply ]