E - Tile Distance 2 解説 by evima
Another ExplanationThe tile with the bottom-left vertex at \((x, y)\) is called \([x, y]\).
The point \((x + 0.5, y + 0.5)\) is in tile \([x, y]\) if \(x\) and \(y\) have the same parity, and in tile \([x - 1, y]\) otherwise. Below, we consider the movement from \([s_x, s_y]\) to \([t_x, t_y]\).
First, by translating both \([s_x, s_y]\) and \([t_x, t_y]\) by \((-s_x, -s_y)\), we can reduce the problem to considering the movement from \([0, 0]\) to \([t_x - s_x, t_y - s_y]\). Below, we consider the movement from \([0, 0]\) to \([x, y]\).
Considering a line parallel to the x-axis passing through the center of tile \([0, 0]\), the tiling is symmetric with respect to this line. Therefore, if \(y < 0\), we can instead consider the movement from \([0, 0]\) to \([x, -y]\), ensuring \(y \geq 0\). Similarly, we can ensure \(x \geq 0\).
Please see the diagram below.
The minimum number of tile-to-tile moves required to go from \([0, 0]\) to \([x, y]\) can be determined by noting that a total of \(y\) moves in the upward directions (up-right and up-left) are needed. If \(y \geq x\), no moves to the right are needed, so the answer is \(y\). Otherwise, \((x - y) / 2\) moves to the right (this value is an integer) are needed, so the answer is \(y + (x - y) / 2\).
Sample implementation (Python)
Sx, Sy = map(int, input().split())
Tx, Ty = map(int, input().split())
Sx -= (Sy - Sx) % 2
Tx -= (Ty - Tx) % 2
Tx -= Sx
Ty -= Sy
Tx = abs(Tx)
Ty = abs(Ty)
print(Ty + max(0, Tx - Ty) // 2)
投稿日時:
最終更新: