Acdream semi-finals guide series (6)

Source: Internet
Author: User
Problem description

Sudoku is a popular single player game. the objective is to fill a 9x9 matrix with digits so that each column, each row, and all 9 non-overlapping 3x3 sub-matrices contain all of the digits from 1 through 9. each 9x9 matrix is partially completed at the start of game play and typically has a unique solution.

Given a completedN2 × N2Sudoku matrix, your task is to determine whether it is a valid solution.

A valid solution must satisfy the following criteria:

  • Each row contains each numberFrom 1 to N2, Once each.
  • Each column contains each numberFrom 1 to N2, Once each.
  • DivideN2 × N2MatrixN2Non-overlappingN × nSub-matrices. Each sub-matrix contains each numberFrom 1 to N2, Once each.

You don't need to worry about the uniqueness of the problem.Just check if the given matrix is a valid solution.

Input

The first line of the input gives the number of test cases,T (1 ≤ t ≤100).

TTest Cases Follow. Each test case starts with an integerN (3 ≤ n ≤ 6).

The nextN2Lines describe a completed Sudoku solution, with each line contains exactlyN2Integers.

All input integers are positive and less1000.

Output

For each test case, output one line containing "case # X: Y", whereXIs the case number (starting from 1) andYIs"Yes"(Quotes for clarity only) if it is a valid solution, or"No"(Quotes for clarity only) if it is invalid.

Sample Input
335 3 4 6 7 8 9 1 26 7 2 1 9 5 3 4 81 9 8 3 4 2 5 6 78 5 9 7 6 1 4 2 34 2 6 8 5 3 7 9 17 1 3 9 2 4 8 5 69 6 1 5 3 7 2 8 42 8 7 4 1 9 6 3 53 4 5 2 8 6 1 7 931 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 935 3 4 6 7 8 9 1 26 7 2 1 9 5 3 4 81 9 8 3 4 2 5 6 78 5 9 7 6 1 4 2 34 2 6 8 999 3 7 9 17 1 3 9 2 4 8 5 69 6 1 5 3 7 2 8 42 8 7 4 1 9 6 3 53 4 5 2 8 6 1 7 9
Sample output
Case #1: YesCase #2: NoCase #3: No
# Include <iostream> using namespace STD; int N; int small [40] [40]; int Col [40] [40]; int row [40] [40]; int main () {int t; CIN> T; int A; int K; For (int count = 0; count <t; count ++) {bool flag = false; cin> N; memset (small, 0, sizeof (small); memset (COL, 0, sizeof (COL); memset (row, 0, sizeof (ROW); For (INT I = 0; I <n * n; I ++) {for (Int J = 0; j <n * N; j ++) {k = (I/n) * n + J/N; // the rule here is very important !! Cin> A; if (a <1 | A> N * n) {flag = true; continue ;} // check each row if (row [I] [a]) {flag = true;} else row [I] [a] = true; // check each column if (COL [J] [a]) {flag = true;} else Col [J] [a] = true; // check each small rectangle. If (small [k] [a]) {flag = true ;} else {small [k] [a] = true ;}} if (! Flag) printf ("case # % d: Yes \ n", count); else printf ("case # % d: NO \ n", count);} return 0 ;}









Acdream semi-finals guide series (6)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.