UVa703 - Triple Ties: The Organizer's Nightmare

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), where i 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 positiveinteger N 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, if M>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 

3
0 0 1
1 0 0
0 1 0
3
0 1 0
0 0 0
0 0 0
3
0 0 0
0 0 0
0 0 0

Sample Output 

1
3 2 1
0
1
1 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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值