Under construction

Still being built. Things may change

Sudoku Solution Checker

Interview

Time limit5sMemory limit512 MB

Summary
Check whether each completed N-squared by N-squared grid has every row, column, and N by N block holding 1 to N-squared exactly once.
Level

Easy3 of 10

Topics
Matrix, Hash map, Implementation
Solved
No attempts yet

Problem

Sudoku is a single player number puzzle. You fill a 9x9 grid with digits so that every row, every column, and each of the nine non-overlapping 3x3 sub-matrices holds each digit from 1 to 9 exactly once. The grid starts out partially filled and usually has one solution.

You are given a completed Sudoku grid of size N2×N2N^2 \times N^2. Decide whether it is a valid solution. A valid solution meets all three rules below.

  • Every row holds each number from 1 to N2N^2 exactly once.
  • Every column holds each number from 1 to N2N^2 exactly once.
  • Split the N2×N2N^2 \times N^2 grid into N2N^2 non-overlapping N×NN \times N sub-matrices. Every sub-matrix holds each number from 1 to N2N^2 exactly once.

Uniqueness of the puzzle does not matter here. Only check whether the given grid is a valid solution.

Input

The first line holds the number of test cases TT. TT test cases follow. Each test case starts with an integer NN. The next N2N^2 lines describe a completed Sudoku grid, and each line holds exactly N2N^2 integers. Every integer in the input is positive and smaller than 1000.

Limits

  • 1≤T≤1001 \le T \le 100
  • 3≤N≤63 \le N \le 6

Output

For each test case print one line in the form Case #x: y, where x is the test case number starting from 1 and y is Yes if the grid is a valid solution or No if it is not. The judge is case sensitive, so yes and no are not accepted.

Examples1

  1. Example 1

    Input
    3
    3
    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
    3
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9
    3
    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 999 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
    
    Expected output
    Case #1: Yes
    Case #2: No
    Case #3: No