AcWing 703 Sudoku Check

Title description:

Sudoku is a popular single player game.

The goal is to fill the 9x9 matrix with numbers so that each column, each row and all 9 non-overlapping 3x3 sub-matrices contain all numbers from 1 to 9.

Each 9x9 matrix will have some numbers already given at the beginning of the game, and there is usually a unique solution.

1.png

2.png

Given the completed N^2∗N^2 Sudoku matrix, your task is to determine whether it is a valid solution.

An effective solution must meet the following conditions:

  • Each line contains each number from 1 to N^2, once for each number.
  • Each column contains each number from 1 to N^2, once for each number.
  • Divide the N^2∗N^2 matrix into N^2 non-overlapping N∗N sub-matrices. Each sub-matrix contains every number from 1 to N^2, once for each number.

You don't need to worry about the uniqueness of the problem, just check whether the given matrix is ​​an effective solution.

Input format

The first line contains an integer T, which means there are T groups of test data.

The first row of each group of data contains the integer N.

The next N^2 lines, each line contains N^2 numbers (none of which exceeds 1000), which are used to describe the complete Sudoku matrix.

Output format

Each group of data outputs a result, and each result occupies one line.

The result is expressed as "Case #x: y", where x is the group number (starting from 1), if the given matrix is ​​a valid solution, then y is Yes, otherwise y is No.

data range

1≤T≤100,
3≤N≤6

Input sample:

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

Sample output:

Case #1: Yes
Case #2: No
Case #3: No
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;
const int MAX = 50;

int a[MAX][MAX];
int n, T, N;
bool row[MAX][MAX], col[MAX][MAX], tip[MAX][MAX][MAX];

bool judge()
{
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            int x = a[i][j];
            if(x >= 1 && x <= N && !row[i][x] && !col[j][x] && !tip[i / n][j / n][x])
            {
                row[i][x] = col[j][x] = tip[i / n][j / n][x] = true;
            }
            else
                return false;

        }
    }
    return true;
}

int main()
{
    scanf("%d", &T);

    int k = 1;

    while(T--)
    {
        memset(row, false, sizeof(row));
        memset(col, false, sizeof(col));
        memset(tip, false, sizeof(tip));

        scanf("%d", &n);
        N = n * n;

        for(int i = 0; i < N; i++)
        {
            for(int j = 0; j < N; j++)
                scanf("%d", &a[i][j]);
        }

        if(judge())
            printf("Case #%d: Yes\n", k++);
        else
            printf("Case #%d: No\n", k++);
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44620183/article/details/113779247