P1443 Knight's Traversal

Description

There is an $n \times m$ chessboard. A knight is at $(x, y)$. Compute, for every square on the board, the minimum number of moves the knight needs to reach it.

Input Format

A single line containing four integers $n, m, x, y$.

Output Format

An $n \times m$ matrix where each entry is the minimum number of moves for the knight to reach that square (output $-1$ if it is unreachable).

Explanation/Hint

Constraints and Conventions For all test points, it is guaranteed that $1 \leq x \leq n \leq 400$, $1 \leq y \leq m \leq 400$. After August 2022, the requirement on fixed field width in the output was removed. For compatibility, outputs where each integer is separated by spaces or by reasonable field widths will both be judged correct. Translated by ChatGPT 5