Comment detail

情報オリンピック2006年度国内本選問題4 (Nested Flatten)

パターン総当りで解いて見ました。

  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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Sample146 {
    enum Issue {
        Unknown, Win, Lose,
    }

    private final int n;
    private final Issue[][] table;

    public Sample146(int n) {
        this.n = n;
        table = new Issue[n][];
        for (int index = 0; index < n; index++) {
            table[index] = new Issue[n];
            Arrays.fill(table[index], Issue.Unknown);
        }
    }

    public void add(int win, int lose) {
        table[win - 1][lose - 1] = Issue.Win;
        table[lose - 1][win - 1] = Issue.Lose;
    }

    public int[] calc() {
        List<Issue[][]> tableList = createAllTable();
        List<int[]> resultList = createResultList(tableList);

        int[] result = resultList.get(0);
        result[n] = (resultList.size() > 1)? 1: 0;
        return result;
    }

    private List<Issue[][]> createAllTable() {
        List<Issue[][]> tableList = new ArrayList<Issue[][]>();
        tableList.add(copyTable(table));
        for (int team = 0; team < n - 1; team++) {
            for (int opp = team + 1; opp < n; opp++) {
                if (table[team][opp] == Issue.Unknown) {
                    for (Issue[][] res: tableList.toArray(new Issue[0][][])) {
                        res[team][opp] = Issue.Win;
                        res[opp][team] = Issue.Lose;

                        Issue[][] copy = copyTable(res);
                        copy[team][opp] = Issue.Lose;
                        copy[opp][team] = Issue.Win;
                        tableList.add(copy);
                    }
                }
            }
        }
        return tableList;
    }
    private Issue[][] copyTable(Issue[][] table) {
        Issue[][] copy = new Issue[table.length][];
        int index = 0;
        for (Issue[] row: table) {
            copy[index++] = copyRow(row);
        }
        return copy;
    }
    private Issue[] copyRow(Issue[] row) {
        Issue[] copy = new Issue[row.length];
        int index = 0;
        for (Issue issue: row) {
            copy[index++] = issue;
        }
        return copy;
    }
    private List<int[]> createResultList(List<Issue[][]> tables) {
        List<int[]> list = new ArrayList<int[]>();
        OUTER: for (Issue[][] table: tables) {
            int[] count = new int[n];
            for (int team = 0; team < table.length; team++) {
                for (Issue issue: table[team]) {
                    if (issue == Issue.Win) count[team]++;
                }
            }
            int[] result = new int[n + 1];
            for (int team = 0; team < n; team++) {
                result[n - 1 - count[team]] = team + 1;
            }
            for (int order = 0; order < n; order++) {
                if (result[order] == 0) continue OUTER;
            }
            list.add(result);
        }
        return list;
    }


    private static int[] toIntArray(String[] input) {
        int[] result = new int[input.length];
        int index = 0;
        for (String s : input) {
            result[index++] = Integer.parseInt(s);
        }
        return result;
    }

    public static void main(String[] args) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        try {
            int n = Integer.parseInt(reader.readLine());
            Sample146 sample = new Sample146(n);
            int m = Integer.parseInt(reader.readLine());
            for (int index = 0; index < m; index++) {
                int[] array = toIntArray(reader.readLine().split("\\s+"));
                sample.add(array[0], array[1]);
            }
            int[] result = sample.calc();
            for (int i: result) {
                System.out.println(i);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Index

Feed

Other

Link

Pathtraq

loading...