Katkat

Find the Guaranteed Capture Time in a Turn-Based Graph Game

ZipHQSoftware Engineer · Technical Screen

Last updated

Solve guaranteed capture time in a finite turn-based graph pursuit game, including Jerry-first moves, staying, optimal adversarial play, and infinite evasion on cycles.

Tom and Jerry move on a finite undirected graph. Their starting vertices are given and both always know the current positions. Each second, Jerry moves first to a neighbor or stays; Tom then moves to a neighbor or stays. Capture occurs as soon as they occupy the same vertex.

Assuming Tom minimizes capture time and Jerry maximizes it, return the smallest number of seconds in which Tom can guarantee capture, or -1 if Jerry can evade forever.

Implement guaranteed_capture_time(n: int, edges: int[][], tom: int, jerry: int) -> int.

Constraints & Assumptions

  • 1 <= n <= 50; vertices are 0..n-1. Edges are unique undirected pairs of distinct valid vertices. Disconnected graphs are allowed.
  • Each player may stay in place every turn. There are no blocked rooms beyond missing edges and no random choices after the initial positions are supplied.
  • If initial positions are equal, return 0. Otherwise, capture during either player's move in the first second counts as 1 second, and similarly for later seconds.
  • Both players choose optimally with full state knowledge. A move onto the other player causes immediate capture; there is no edge-crossing event between simultaneous moves because moves are sequential.
  • Finiteness alone does not guarantee capture. This explicit -1 outcome preserves the general-graph rules instead of silently assuming a tree or another missing restriction.

Examples

n = 3, edges = [[0,1],[1,2]], tom = 0, jerry = 2
result = 2
n = 4, edges = [[0,1],[1,2],[2,3],[3,0]], tom = 0, jerry = 2
result = -1

On the cycle, Jerry can maintain an escape indefinitely under these rules even though the graph is finite.

The report describes a hint based on comparing BFS distances from the two starting points. Explain why those distances alone do not solve this unrestricted adversarial game, and what additional graph restriction or changed objective would be needed before relying on such a shortcut. Your function must solve the stated game, including cycles and infinite evasion.

Hints

Hint 1

Include whose move is next: A game state depends on both positions and the turn phase. A capture guarantee requires one successful choice for Tom but must survive every legal choice Jerry can make.