#C7063. Special Submatrix Detection
Special Submatrix Detection
Special Submatrix Detection
You are given an integer n and an n × n matrix of integers. A matrix is called a Latin square if each row and each column contains all the integers from 1 to n exactly once.
In this problem, a special submatrix is defined as a contiguous square submatrix (i.e. a submatrix formed by selecting a block of adjacent rows and adjacent columns) which exactly equals the entire matrix in terms of the expected permutation. In other words, the only candidate submatrix that can be special is the full matrix itself. (Note: Although one might consider smaller 1×1 submatrices, for the purpose of this problem the special submatrix must be of order n so that every row and every column, when sorted, is equal to \(\{1,2,\dots,n\}\).)
Your task is to determine if the given matrix is a Latin square. If it is, output YES
; otherwise, output NO
.
Note: The input is given for multiple test cases. For each test case, you need to check the condition separately.
inputFormat
The input begins with a single integer t (\(1 \le t \le 100\)) denoting the number of test cases. The description of each test case follows.
Each test case starts with an integer n (\(1 \le n \le 100\)) on a new line, representing the order of the matrix. This is followed by n lines, each containing n space‐separated integers. It is guaranteed that each integer is between 1 and n (inclusive).
outputFormat
For each test case, print a single line containing YES
if the matrix is a Latin square (i.e. the full matrix is a special submatrix), and NO
otherwise.
4
3
2 3 1
1 2 3
3 1 2
3
1 2 3
1 2 3
1 2 3
1
1
4
2 3 4 1
1 4 3 2
4 2 1 3
3 1 2 4
YES
NO
YES
YES
</p>