KatInterview

Complete a Partially Filled Number Grid with Row, Column, Box Rules

Arrays & HashingAuto-graded · 13 test cases
HardAuto-gradedArrays & Hashingbacktrackingconstraint-satisfactionbitmask

Last updated

Fill in the blanks of a 9x9 digit grid so every row, column and 3x3 block holds each digit 1-9 once, testing backtracking with constraint pruning.

You are given a 9×9 grid that represents a partly completed number-placement puzzle. Each cell holds either a digit character '1' through '9' or the character '.', which marks a blank cell.

Your task is to replace every blank with a digit so that the finished grid obeys all three rules:

  • Every row contains each of the digits 1 to 9 exactly once.
  • Every column contains each of the digits 1 to 9 exactly once.
  • Each of the nine 3×3 blocks (the grid split into three bands of three rows and three bands of three columns) contains each of the digits 1 to 9 exactly once.

The digits already present in the input must stay unchanged.

Input

  • grid: a list of 9 lists, each with 9 single-character strings. Each string is '1'..'9' or '.'.

Output

  • Return the completed 9×9 grid in the same format (a list of 9 lists of 9 single-character digit strings). You may also fill the input in place, but the returned value must be the finished grid.

Constraints

  • The grid is always exactly 9×9.
  • Every cell is one of '1'..'9' or '.'.
  • The given digits do not contradict each other.
  • At least one valid completion exists, and the puzzle is treated as having a single valid completion.
  • A grid with no blanks is already complete and is returned unchanged.

Examples

Example 1

Input: complete_number_grid([['5', '3', '.', '.', '7', '.', '.', '.', '.'], ['6', '.', '.', '1', '9', '5', '.', '.', '.'], ['.', '9', '8', '.', '.', '.', '.', '6', '.'], ['8', '.', '.', '.', '6', '.', '.', '.', '3'], ['4', '.', '.', '8', '.', '3', '.', '.', '1'], ['7', '.', '.', '.', '2', '.', '.', '.', '6'], ['.', '6', '.', '.', '.', '.', '2', '8', '.'], ['.', '.', '.', '4', '1', '9', '.', '.', '5'], ['.', '.', '.', '.', '8', '.', '.', '7', '9']])

Output: [['5', '3', '4', '6', '7', '8', '9', '1', '2'], ['6', '7', '2', '1', '9', '5', '3', '4', '8'], ['1', '9', '8', '3', '4', '2', '5', '6', '7'], ['8', '5', '9', '7', '6', '1', '4', '2', '3'], ['4', '2', '6', '8', '5', '3', '7', '9', '1'], ['7', '1', '3', '9', '2', '4', '8', '5', '6'], ['9', '6', '1', '5', '3', '7', '2', '8', '4'], ['2', '8', '7', '4', '1', '9', '6', '3', '5'], ['3', '4', '5', '2', '8', '6', '1', '7', '9']]

Hints

Hint 1

Keep track of which digits are already used in each row, each column and each 3x3 block. A cell's legal digits are those missing from all three of its groups.

Hint 2

Use plain backtracking: choose a blank, try each legal digit, recurse, and undo the placement if the recursion fails. Make sure you undo both the grid cell and your bookkeeping.

Hint 3

Speed up the search by always picking the blank with the fewest legal digits. A blank with zero options means you should backtrack immediately, and one with a single option is a forced move. Bitmasks make the checks O(1).