Generalized chess knight

Let us define an (a, b) knight as a piece that moves by jumping a cells
in one direction and b cells in the other direction, where the possible
directions are horizontal and vertical. For instance, the traditional
chess knight is a (1, 2) knight.

Given an n × m board with obstacles, an initial position (i₁, j₁), a
final position (i₂, j₂), and the pair (a, b), can you tell if an (a, b)
knight initially located at (i₁, j₁) can reach (i₂, j₂) in two or less
steps? The knight can never leave the board, nor visit any obstacles.

Input

Input consists of several cases, each with n and m, followed by the
board (n lines with m characters each, where an ‘X’ indicates an
obstacle and a ‘.’ indicates a free cell), followed by i₁, j₁, i₂, j₂, a
and b. Assume that n and m are between 1 and 42, that (i₁, j₁) and
(i₂, j₂) are free positions inside the board, and 1 ≤ a < b ≤ 5. The
upper-left position is (0, 0).

Output

For every case, print “yes” or “no” depending on whether the goal
position is reachable from the initial position in two or less steps.

Problem information

Author: Enric Cusell

Generation: 2026-01-25T10:16:38.644Z

© Jutge.org, 2006–2026.
https://jutge.org
