Sudoku checker
Time limit:2000/1000 ms (Java/Others)
Memory limit:128000/64000 KB (Java/others) submitstatusproblem 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
Question: Give a Sudoku to see if the operator does not meet the requirements.
PS: I have made a long pose for a question. I have only heard of Data independence before, but I have never played it. This time, I have understood this rule. Haha, I have gained a lot.
Well, let's introduce the rules. Now we have the number of independence numbers for N ^ 2 * n ^ 2 to determine whether the rule meets the requirements:
1. Use 1 ~ for each line ~ The number of N ^ 2 is filled up, and each number appears only once, that is, 1 ~ N ^ 2 is arranged to each row;
2. The requirements for rows are the same as those for columns;
3. The elements of the N ^ 2 N * n rectangles that are all divided must also be 1 ~ N ^ 2.
AC code:
# Include <cstdio> # include <algorithm> # include <cstring> using namespace STD; int A [40] [40], B [40] [1000], c [40] [1000], d [1000]; int t, t, n; int main () {# ifndef online_judgefreopen ("in.txt", "r", stdin ); # endifscanf ("% d", & T); For (int t = 1; t <= T; t ++) {memset (B, 0, sizeof (B); memset (C, 0, sizeof (c); scanf ("% d", & N); For (INT I = 1; I <= N * n; I ++) {// process the row and column for (Int J = 1; j <= N * n; j ++) {scanf ("% d", & A [I] [J]); B [I] [A [I] [J] ++; C [J] [A [I] [J] ++ ;}} int flag = 1; for (INT I = 1; I <= N * n; I ++) {// judge the row and column for (Int J = 1; j <= N * n; j ++) {If (B [I] [J]! = 1 | C [I] [J]! = 1) {flag = 0; break ;}}if (FLAG) {for (INT I = 1; I <= N; I ++) {// determine each n * n matrix for (Int J = 1; j <= N; j ++) {memset (D, 0, sizeof (d )); for (int K = 1; k <= N; k ++) {for (int s = 1; S <= N; s ++) {d [A [(I-1) * n + k] [(J-1) * n + S] + + ;}} for (int K = 1; k <= N * n; k ++) {If (d [k]! = 1) {flag = 0; break;} If (! Flag) break;} If (! Flag) Break ;}}if (FLAG) printf ("case # % d: Yes \ n", T); else printf ("case # % d: NO \ n ", t);} return 0 ;}
Acdream 1195 Sudoku Checker (violent)