Under construction

Still being built. Things may change

Killer Sudoku

Time limit1sMemory limit512 MB

Summary
Read a 19 by 37 ASCII diagram of a Killer Sudoku grid plus one sum per cage, and report OK or NotOK for all constraints.
Level

Medium5 of 10

Topics
Implementation, Simulation, Array, Hash map
Solved
No attempts yet

Problem

A standard Sudoku puzzle is a 9×99 \times 9 grid of squares filled in with the numbers 1 to 9, one number per square, so that each of the nine rows, nine columns and nine major sub-grids holds a permutation of the numbers 1 to 9. The major sub-grids are the nine 3×33 \times 3 sub-grids that make up the overall 9×99 \times 9 grid. A standard Sudoku puzzle starts with some numbers already placed in their squares, and the aim is to fill in the rest of the puzzle so that the row, column and major sub-grid constraints hold. Figure K.1 is a solved standard Sudoku.

Killer Sudoku is a variant on the standard form. Instead of starting with some individual numbers already placed, the grid is divided into one or more connected regions known as cages, and the numbers that may appear in a cage are constrained. Each cage has a total that the number or numbers in the cage must sum to. In the sub-variant considered here, the numbers in a cage must also be a set, so no number is repeated inside a cage.

Figure K.1: A standard Sudoku.

Figure K.2: A Killer Sudoku grid (left) with a solution (right).

You are given a completed grid. Check whether it meets the row, column, major sub-grid and cage constraints of this sub-variant. There is no guarantee that the cages in a grid make the grid soluble as a Killer Sudoku puzzle.

Input

The input starts with a diagram of the grid. The diagram is a 19 by 37 array of characters.

The grid is a 9×99 \times 9 grid of (not quite square) squares. A square, including its border, consists of 3 vertically aligned rows of 5 characters each. The border of each square is shared with the adjacent squares where they exist, and the border is filled in around the edge of the grid, as though the exterior were the boundary of an extra cage. The interior of each square is a row of 3 characters: a space, the integer NN (1≤N≤91 \le N \le 9) in the square, and another space.

The 4 border corners of a square are each a plus (+) character. Each border side between vertically adjacent squares is 3 characters between the corresponding corners. These 3 characters are all spaces if the two squares sharing the border are in the same cage, and all hyphens (-) otherwise. Each border side between horizontally adjacent squares is a single character, under one corner and above another. This character is a space if the squares sharing the border are in the same cage, and a vertical bar (|) otherwise.

+---+---+---+---+---+---+---+---+---+      +---+---+---+---+---+---+---+---+---+
| 0   0   0   0   0   0   0   0   0 |      | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+   +   +   +   +   +   +   +   +   +      +---+---+---+---+---+---+---+---+---+
| 0   0   0   0   0   0   0   0   0 |      | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+   +   +   +   +   +   +   +   +   +      +---+---+---+---+---+---+---+---+---+
| 0   0   0   0   0   0   0   0   0 |      | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+   +   +   +   +   +   +   +   +   +      +---+---+---+---+---+---+---+---+---+
| 0   0   0   0   0   0   0   0   0 |      | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+   +   +   +   +   +   +   +   +   +      +---+---+---+---+---+---+---+---+---+
| 0   0   0   0   0   0   0   0   0 |      | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+   +   +   +   +   +   +   +   +   +      +---+---+---+---+---+---+---+---+---+
| 0   0   0   0   0   0   0   0   0 |      | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+   +   +   +   +   +   +   +   +   +      +---+---+---+---+---+---+---+---+---+
| 0   0   0   0   0   0   0   0   0 |      | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+   +   +   +   +   +   +   +   +   +      +---+---+---+---+---+---+---+---+---+
| 0   0   0   0   0   0   0   0   0 |      | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+   +   +   +   +   +   +   +   +   +      +---+---+---+---+---+---+---+---+---+
| 0   0   0   0   0   0   0   0   0 |      | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+      +---+---+---+---+---+---+---+---+---+

Figure K.3: The general structure for the grid. Each 0 will be an integer from 1 to 9. The left is the minimal case with one large cage, and the right is the maximal case with 81 small cages.

The diagram is followed by one line for each cage in it. Each line contains three integers RR (1≤R≤91 \le R \le 9), CC (1≤C≤91 \le C \le 9) and SS (1≤S≤451 \le S \le 45). RR and CC give the row and the column of one square within the cage, and SS gives the value that the number or numbers within the cage must sum to.

The rows are numbered from 1 at the top to 9 at the bottom. The columns are numbered from 1 at the left to 9 at the right. The input contains exactly one constraint per cage.

Output

Display OK if all of the constraints are met. Otherwise, display NotOK. There is no whitespace between Not and OK.

Note

The grid of the first example is Figure K.2. The grid of the second example is Figure K.1 with the centre entry changed to a 4, and its cages match the major sub-grid constraints of a standard Sudoku.

Examples2

  1. Example 1

    Input
    +---+---+---+---+---+---+---+---+---+
    | 1 | 2 | 3 | 4 | 5 | 6 | 7   8   9 |
    +   +   +   +   +   +   +---+---+---+
    | 4 | 5 | 6 | 7 | 8 | 9 | 1   2   3 |
    +   +---+---+---+---+---+---+---+   +
    | 7 | 8   9   1 | 2   3   4 | 5 | 6 |
    +---+---+---+---+---+---+---+   +---+
    | 2 | 3   1 | 5   6 | 4   8 | 9 | 7 |
    +   +---+---+---+---+---+   +---+   +
    | 5 | 6   4 | 8   9 | 7 | 2 | 3 | 1 |
    +---+---+---+---+---+   +---+   +---+
    | 8   9 | 7   2 | 3   1   5 | 6 | 4 |
    +---+---+---+---+---+---+---+---+   +
    | 3   1   2 | 6   4 | 5   9 | 7   8 |
    +---+---+---+---+---+---+---+   +   +
    | 6   4   5 | 9   7   8 | 3 | 1   2 |
    +---+---+---+---+---+   +   +---+---+
    | 9   7 | 8   3   1 | 2 | 6   4   5 |
    +---+---+---+---+---+---+---+---+---+
    9 8 18
    2 1 12
    1 2 7
    1 3 9
    1 4 11
    1 5 13
    1 6 15
    1 8 24
    2 7 12
    3 2 18
    3 5 9
    3 8 14
    4 1 7
    4 2 4
    4 4 11
    4 6 14
    4 9 8
    5 2 10
    5 4 17
    5 6 16
    5 8 9
    6 1 17
    6 3 9
    6 9 22
    7 1 6
    7 4 10
    7 6 14
    8 1 15
    8 4 26
    9 1 16
    9 3 12
    
    Expected output
    OK
    
  2. Example 2

    Input
    +---+---+---+---+---+---+---+---+---+
    | 2   9   5 | 7   4   3 | 8   6   1 |
    +   +   +   +   +   +   +   +   +   +
    | 4   3   1 | 8   6   5 | 9   2   7 |
    +   +   +   +   +   +   +   +   +   +
    | 8   7   6 | 1   9   2 | 5   4   3 |
    +---+---+---+---+---+---+---+---+---+
    | 3   8   7 | 4   5   9 | 2   1   6 |
    +   +   +   +   +   +   +   +   +   +
    | 6   1   2 | 3   4   7 | 4   9   5 |
    +   +   +   +   +   +   +   +   +   +
    | 5   4   9 | 2   1   6 | 7   3   8 |
    +---+---+---+---+---+---+---+---+---+
    | 7   6   3 | 5   2   4 | 1   8   9 |
    +   +   +   +   +   +   +   +   +   +
    | 9   2   8 | 6   7   1 | 3   5   4 |
    +   +   +   +   +   +   +   +   +   +
    | 1   5   4 | 9   3   8 | 6   7   2 |
    +---+---+---+---+---+---+---+---+---+
    2 2 45
    5 2 45
    8 2 45
    2 5 45
    5 5 45
    8 5 45
    2 8 45
    5 8 45
    8 8 45
    
    Expected output
    NotOK