#P1443. 马的遍历

马的遍历

Description

There is an n×mn \times m chessboard. A knight is at (x,y)(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,yn, m, x, y.

Output Format

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

3 3 1 1

0 3 2    
3 -1 1    
2 1 4    

Hint

Constraints and Conventions

For all test points, it is guaranteed that 1xn4001 \leq x \leq n \leq 400, 1ym4001 \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