UVa703 - Triple Ties: The Organizer's Nightmare

来源:互联网 发布:粉多多类似软件 编辑:程序博客网 时间:2024/05/01 00:04

Triple Ties: The Organizer's Nightmare 

N participants compete in a chess tournament, every playercompeting against each other one single time. The results are registered in a square matrix in the following way:

  • If player i beats player j, the digit 1 must be written at the position (i,j), wherei denotes the row and j de column, and the digit 0 at the place (j,i).

  • If the game ends in a draw, both numbers are zero.

  • The digits at the main diagonal are also zero.

To establish the final clasification the organizer must face the problem ofpossible ties. The general criterion is that ties should be solved by countingthe points obtained in the matches between the tied players, but sometimesthis does not solve the problem. This happens for two players if they reacheda draw in their respective match; for three tied players, there exist twodifferent undecidable situations:

  • If the three matches were draws.

  • If there is a cyclic chain of victories, such as 1 beats 2, 2 beats 3, 3beats 1.

Since the organizer wishes to establish different classifications according toage groups, nationality, etc. of the participants, it has been decided toelaborate a table containing all the triple ties. If all three matches end indraws, the three numbers must be annotated in increasing order, and if thereis a cycle of victories, in the following order: the first in the line beatsthe second, and the second beats the third one. Among the three possibilitiesof expressing this fact, the line must be written in increasing or decreasingorder. These three-number chains must themselves be listed in lexicographicalorder.

Input 

Unlimited number of tests. Each test consists in a line with a positiveintegerN representing the number of players and N lines containing theresults matrix of the tournament with consecutive digits separated by a blankspace ($3 \le N \le 100$).

Output 

For every input a line containing the total amount M of undecidable tripleties; and, ifM>0, M lines, each of them with the three numbers of theinvolved players separated by a blank space and satisfying the orderrequirements, both within the line and in the set of lines.

Sample Input 

30 0 11 0 00 1 030 1 00 0 00 0 030 0 00 0 00 0 0

Sample Output 

13 2 1011 2 3

import java.io.FileInputStream;import java.io.InputStreamReader;import java.io.BufferedReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.StreamTokenizer;import java.util.ArrayList;public class Main {public static final boolean DEBUG = false;public static final int N = 110;public StreamTokenizer tokenizer;public BufferedReader cin;public PrintWriter cout;public int n;public int[][] win = new int[N][N];static class Node{int i, j, k;}public void init() {try {if (DEBUG) {cin = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\OJ\\uva_in.txt")));} else {cin = new BufferedReader(new InputStreamReader(System.in));}cout = new PrintWriter(new OutputStreamWriter(System.out));tokenizer = new StreamTokenizer(cin);} catch (Exception e) {e.printStackTrace();}}public String next() {try {tokenizer.nextToken();if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;else if (tokenizer.ttype == StreamTokenizer.TT_WORD) return tokenizer.sval; else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) return String.valueOf((int)tokenizer.nval);else return null;} catch (Exception e) {e.printStackTrace();return null;}}public boolean input(){try {String s = next();if (s == null) return false;n = Integer.parseInt(s);for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {win[i][j] = Integer.parseInt(next());}}return true;} catch (Exception e) {e.printStackTrace();return false;}}public void solve() {ArrayList<Node> ans = new ArrayList<Node>();for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {for (int k = 1; k <= n; k++) {if (((i < j && j < k) || (i > j && j > k)) && (win[i][j] != 0 && win[j][k] != 0 && win[k][i] != 0)) {Node tmp = new Node();tmp.i = i;tmp.j = j;tmp.k = k;ans.add(tmp);}if ((i < j && j < k) && (win[i][j] == 0 && win[j][i] == 0 && win[j][k] == 0 && win[k][j] == 0&& win[k][i] == 0 && win[i][k] == 0)) {Node tmp = new Node();tmp.i = i;tmp.j = j;tmp.k = k;ans.add(tmp);}}}}cout.println(ans.size());for (int i = 0, size = ans.size(); i < size; i++) {Node tmp = ans.get(i);cout.println(tmp.i + " " + tmp.j + " " + tmp.k);}cout.flush();}public static void main(String[] args) {Main solver = new Main();solver.init();while (solver.input()) {solver.solve();}}}



0 0
原创粉丝点击