Duplicates

시간 제한2초메모리 제한1024 MB

요약
각 값이 1부터 n인 n x n 행렬이 주어질 때, 모든 행과 열이 같은 값을 두 번 이상 포함하도록 고쳐야 하는 최소 항목 수를 구한다.
난이도

보통10점 중 5점

유형
그리디, 구현, 행렬, 조합론
정답자
아직 제출이 없습니다

문제

We say that a number sequence contains duplicates if there is an element that appears more than once in the sequence. Formally, a sequence (a_1,…,a_n)(a\_1, \dots , a\_n) contains duplicates if there exist two indices ii and jj such that i≠ji \ne j and a_i=a_ja\_i = a\_j.

You are given an n×nn \times n matrix XX. Each entry in XX is an integer between 11 and nn, inclusive. You can modify zero or more entries in XX to arbitrary integers between 11 and nn, inclusive. Different entries can be modified to different integers.

Your task is to make modifications to entries of XX such that all of the following hold:

  • For each row ii, the sequence (X_i1,X_i2,…,X_in)(X\_{i1}, X\_{i2}, \dots , X\_{in}) contains duplicates.
  • For each column jj, the sequence (X_1j,X_2j,…,X_nj)(X\_{1j }, X\_{2j}, \dots , X\_{nj}) contains duplicates.

Compute the minimum number of entries that need to be modified to achieve this. Also, find one possible set of modifications to do it. For each modification, you have to specify which entry will be modified and to what value. Note that the minimum number of entries to be modified can be zero when the given matrix XX already satisfies the conditions above.

입력

The first line of input contains one integer tt (1≤t≤10001 ≤ t ≤ 1000) representing the number of test cases. After that, tt test cases follow. Each of them is presented as follows.

The first line of a test case contains one integer nn (3≤n≤1003 ≤ n ≤ 100). Each of the next nn lines contains nn integers. The jj-th integer in the ii-th line denotes X_ijX\_{ij} (1≤X_ij≤n1 ≤ X\_{ij} ≤ n).

The sum of n2n^2 across all test cases in one input file does not exceed 10,00010\\, 000.

출력

For each test case, output a set of modifications in the following format.

On the first line, output an integer mm representing the minimum number of entries that need to be modified. On each of the next mm lines, output three integers ii, jj, and vv. This represents a single modification where the entry X_ijX\_{ij} will be modified to vv. All of the three integers must be between 11 and nn, inclusive.

If there are multiple solutions, you can output any of them.

예제1

  1. 예제 1

    입력
    5
    4
    3 2 1 1
    2 1 3 4
    1 3 3 1
    4 4 4 2
    3
    1 3 1
    2 1 3
    3 2 2
    5
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    3
    1 1 2
    2 2 1
    2 3 2
    3
    1 1 3
    3 2 1
    3 1 3
    
    예상 출력
    2
    2 1 1
    4 2 3
    3
    2 1 3
    2 2 3
    3 3 3
    0
    1
    1 2 2
    1
    2 1 1