#C9187. Chessboard Piece Repositioning
Chessboard Piece Repositioning
Chessboard Piece Repositioning
You are given an n x n chessboard with some pieces placed on it. Each cell of the board is either empty (represented by 0) or contains one of six types of pieces (represented by integers 1 through 6). Your task is to reposition each piece so that no two pieces can directly attack each other. In this simplified scenario, you will reposition all pieces to distinct cells on the main diagonal of the board (i.e., cell (i, i) for some i).
Formally, if the chessboard is represented as a matrix \(A\) of size \(n \times n\), then:
[ A_{ij} = \begin{cases} 0, & \text{if the cell is empty} \ 1 \text{ to } 6, & \text{if a piece is present} \end{cases} ]
You are to determine the minimum number of moves required to reposition the pieces. According to the strategy provided (which is a simplification), the answer is always 1 move, and the repositioned locations are chosen sequentially along the main diagonal.
The output should first print the number of moves, followed by the new positions of each moved piece (one pair per line). Note that the order of pieces is determined by iterating over piece types in increasing numerical order from 1 to 6. For each occurrence of a piece, assign a new position from the main diagonal in the order of occurrence.
inputFormat
The input is read from stdin
and has the following format:
n row1_element1 row1_element2 ... row1_elementn row2_element1 row2_element2 ... row2_elementn ... rown_element1 rown_element2 ... rown_elementn
Here, the first line contains an integer \(n\) (\(1 \le n \le 100\)) representing the size of the chessboard. The following \(n\) lines each contain \(n\) space-separated integers. A value of 0
indicates an empty cell, and a value from 1
to 6
indicates the presence of a chess piece.
outputFormat
The output should be written to stdout
and must follow the format below:
minimum_moves new_row1 new_col1 new_row2 new_col2 ...
The first line contains the minimum number of moves (which, according to the simplified strategy, is always 1). Each subsequent line contains the final position (row and column) of a moved piece. The positions are assigned sequentially along the main diagonal (i.e., \((1, 1)\), \((2, 2)\), etc.) based on the sorted order of piece types from 1
to 6
.
4
0 1 0 0
0 0 6 0
0 2 0 0
3 0 0 4
1
1 1
2 2
3 3
4 4
5 5
</p>