Latin Squares
InterviewTime limit0.5sMemory limit2048 MB
Given T row and column swaps applied in order, output any Latin square of size N that would produce the final matrix, or print * if none exists.
- Level
Easy3 of 10
- Topics
- Array, Simulation, Implementation
- Solved
- No attempts yet
Problem
A Latin square of size is an matrix where each entry is an integer between and , and no two entries in the same row or column are equal.
Alice has a Latin square of size and applied a list of transformations to it. Each transformation swaps either two distinct rows or two distinct columns of the matrix. The transformations are applied in order: the first one is applied to the original matrix, the second to the result of the first, and so on. The final result is an matrix.
Alice does not reveal the matrix, only the list of transformations. Determine whether an original matrix exists that becomes the final matrix after all transformations are applied. If one exists, output any possible content of the original matrix.
Input
The first line contains two integers () and (), the size of the Latin square and the number of transformations.
Each of the next lines describes one transformation, in the order they are applied. A line contains a character and two integers and (, ). If is R, rows and are swapped. If is C, columns and are swapped.
Output
If an original matrix exists that becomes the final matrix after all transformations, output lines with integers each, representing any such matrix.
If no such matrix exists, output a single line containing the character * (asterisk).