KatInterview

Is the Target's Cell Forced by Every Valid Seating?

Trees & GraphsAuto-graded · 25 test cases
MediumAuto-gradedTrees & Graphsbacktrackingconstraint-satisfactiongrid

Last updated

Backtracking over placements of named items on a grid under pairwise relative-position clues, deciding whether the target ends up in one cell across all valid layouts.

Forced Cell on a Grid

You have a grid with rows rows and cols columns. Rows and columns are numbered from 1. Row 1 is the top row and column 1 is the leftmost column. A list of named items must be placed on the grid so that no two items share a cell. Each item occupies exactly one cell, and some cells may stay empty.

A list of clues restricts the placement. Every clue is a list of three strings [relation, a, b], where a and b are item names. The relation compares the position of a with the position of b:

RelationCondition
same_rowrow of a equals row of b
same_columncolumn of a equals column of b
left_ofcolumn of a is strictly less than column of b (rows do not matter)
right_ofcolumn of a is strictly greater than column of b (rows do not matter)
aboverow of a is strictly less than row of b (columns do not matter)
belowrow of a is strictly greater than row of b (columns do not matter)

A placement is valid if all items sit in distinct cells inside the grid and every clue holds.

Write a function that decides whether the cell of the item target is the same in every valid placement.

  • If at least one valid placement exists and all valid placements put target in the same cell, return that cell as [row, column].
  • Otherwise return None.

Only the target's cell has to be forced. Other items may be free to move. For instance, if 50 valid placements exist and all of them put the target at row 2, column 3, the answer is [2, 3].

Return None in each of these situations:

  • No valid placement exists, including contradictory clues.
  • The target can occupy two or more different cells across valid placements.
  • The input is malformed: target is not in entities, the entity names contain duplicates, rows < 1 or cols < 1, there are more entities than cells, or some clue does not have exactly three elements, uses an unknown relation, or mentions an unknown name.

A clue that names the same item twice, such as ["same_row", "A", "A"], is evaluated normally. This one is always true. Strict relations such as ["left_of", "A", "A"] can never hold, so no valid placement exists.

Constraints

  • Entity names are strings. Entities are small in number, so an exhaustive search with pruning is expected.
  • rows and cols are integers, with small values in practice.
  • clues may be empty.
  • The result is a two-element list [row, column] (1-indexed) or None.

Examples

Example 1

Input: forced_cell(2, 2, ['P', 'Q'], [['same_row', 'P', 'Q']], 'P')

Output: None

Example 2

Input: forced_cell(2, 2, ['P', 'Q'], [['left_of', 'P', 'Q']], 'P')

Output: None

Example 3

Input: forced_cell(1, 2, ['P', 'Q'], [], 'P')

Output: None

Hints

Hint 1

Treat this as a constraint-satisfaction problem: assign cells to items one at a time and undo the assignment when you backtrack.

Hint 2

A clue can be checked as soon as both of its items have been placed. If it fails, nothing placed later can fix it, so prune immediately.

Hint 3

You do not need to count every valid placement. Collect the distinct cells the target occupies, and stop as soon as you have seen two different ones.