KatInterview

Shortest Path for a Hobbled Xiangqi Horse on a 10×9 Board

Trees & GraphsModel answer
HardModel answerTrees & GraphsBFSgraph traversalboard games

Last updated

Find the minimum moves for a Chinese-chess horse to reach a target on a 10×9 grid, accounting for the leg-blocking movement restriction and obstacle squares.

Background

In Xiangqi (Chinese chess), the horse moves in a shape similar to a Western knight's "L", but with an extra constraint known as "hobbling the leg" (蹩马腿). Before the horse can complete its jump, it must pass through an intermediate square called the leg. If that leg square is occupied by an obstacle, the entire move in that direction is forbidden.

Movement Rules

From square (r, c), the horse has up to four possible leg directions, each unlocking two jump destinations:

Leg squareReachable destinations
(r-1, c) — up leg(r-2, c-1) and (r-2, c+1)
(r+1, c) — down leg(r+2, c-1) and (r+2, c+1)
(r, c-1) — left leg(r-1, c-2) and (r+1, c-2)
(r, c+1) — right leg(r-1, c+2) and (r+1, c+2)

A move to a destination is only legal when all three of these hold:

  1. The leg square is within the board and is not an obstacle.
  2. The destination square is within the board.
  3. The destination square is not an obstacle.

Task

Given a 10 × 9 board as a 2-D integer array (0 = empty, 1 = obstacle), a start position, and a target position, return the minimum number of moves for the horse to travel from start to target. Return -1 if no legal path exists.

Function Signature

def min_horse_moves(
    board: list[list[int]],
    start: tuple[int, int],
    target: tuple[int, int]
) -> int:
    pass

Constraints

  • len(board) == 10, len(board[0]) == 9
  • board[r][c] ∈ {0, 1}
  • 0 <= start[0], target[0] < 10
  • 0 <= start[1], target[1] < 9
  • If board[start[0]][start[1]] == 1 or board[target[0]][target[1]] == 1, return -1.
  • If start == target and that square is empty, return 0.

Examples

Example 1 — clear board, one move

board  : 10×9 grid of all zeros
start  : (0, 0)
target : (2, 1)
output : 1

The up-leg (1, 0) is empty, so the horse jumps directly to (2, 1).

Example 2 — blocked leg, longer detour

board  : 10×9 grid of all zeros except board[1][0] = 1
start  : (0, 0)
target : (2, 1)
output : 3

The direct jump is forbidden because the leg (1, 0) is blocked.
One valid 3-move path: (0,0) → (1,2) → (3,3) → (2,1).

Hints

Hint 1

Think of every empty board square as a node. Two nodes are connected when the horse can legally jump between them under the leg rule. What algorithm finds shortest paths on an unweighted graph?

Hint 2

Organize your move generator around the four legs, not the eight destinations. For each leg, first check whether that square is clear before you even look at its two destinations. This makes the hobbling rule almost impossible to implement incorrectly.

Hint 3

Mark a square as visited the moment you add it to the queue, not when you pop it. This prevents the same square from being enqueued multiple times at the same depth, which would silently inflate your move count.