#P1443. Minimum Knight Moves on a Chessboard
Minimum Knight Moves on a Chessboard
Minimum Knight Moves on a Chessboard
You are given an \( n\times m \) chessboard and a knight located at the position \( (x, y) \). The knight moves as in classical chess: in an L-shape, i.e., two cells in one direction and one cell in the perpendicular direction. Your task is to compute the minimum number of moves required for the knight to reach every cell on the board starting from \( (x, y) \). Output the results as a matrix where the element at the \( i^{th} \) row and \( j^{th} \) column represents the minimum number of moves required to move from \( (x, y) \) to \( (i, j) \).
The knight's possible moves are given by the following offsets in \( \LaTeX \) format:
[ {(2,1), (2,-1), (-2,1), (-2,-1), (1,2), (1,-2), (-1,2), (-1,-2)} ]
inputFormat
The input consists of two lines:
- The first line contains two integers \( n \) and \( m \) representing the number of rows and columns of the chessboard.
- The second line contains two integers \( x \) and \( y \) representing the starting position of the knight. The board positions are 1-indexed.
outputFormat
Output \( n \) lines, each containing \( m \) space-separated integers. The integer in the \( i^{th} \) row and \( j^{th} \) column should be the minimum number of moves required for the knight to reach position \( (i, j) \) from the starting position \( (x, y) \).
sample
3 3
2 2
2 1 2
1 0 1
2 1 2
</p>