AcWing 703. Sudoku Check

**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.png2.png

Given the completed N2∗N2 Sudoku matrix, your task is to determine whether it is a valid solution.
An effective solution must meet the following conditions:
each row contains each number from 1 to N2, once for each number.
Each column contains each number from 1 to N2, once for each number.
Divide the N2∗N2 matrix into N2 non-overlapping N∗N sub-matrices. Each sub-matrix contains each number from 1 to N2, 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. **

The first line of the input format
contains an integer T, indicating that there are T groups of test data.
The first row of each group of data contains the integer N.
Next N2 rows, each row contains N2 numbers (none of them exceed 1000), which are used to describe the complete Sudoku matrix.

Output format
Each group of data outputs one 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 example:
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

Output example:
Case #1: Yes
Case #2: No
Case #3: No

code show as below:

#include<iostream>
#include <cstring>
using namespace std;
const int N = 40;
int a[N][N];
bool st[N];
int n,m,cnt;
bool check_row()
{
    
    
    for (int i = 0;i<m;i++)
    {
    
    
        memset(st,0,sizeof(st));
        for (int j = 0;j<m;j++)
        {
    
    
            int t = a[i][j];
            if (t <1 || t > m) return false;
            if (st[t]) return false;
            st[t] = true;
        }
    }
    return true;
}

bool check_col()
{
    
    
    for (int i = 0;i<m;i++)
    {
    
    
        memset(st,0,sizeof(st));
        for (int j = 0;j<m;j++)
        {
    
    
            int t = a[j][i];
            if (t <1 || t > m) return false;
            if (st[t]) return false;
            st[t] = true;
        }
    }
    return true;
}

bool check_cell()
{
    
    
    for (int i = 0;i<m;i+=n)
        for(int j = 0;j<m;j+=n)
        {
    
    
            memset(st,0,sizeof(st));
            for (int dx = 0;dx<n;dx++)
                for(int dy = 0;dy<n;dy++)
                {
    
    
                    int t = a[i+dx][j+dy];
                    if (t < 1 || t > m) return false;
                    if (st[t]) return false;
                    st[t] = true;
                }
        }
        return true;
}

int main()
{
    
    
    cin>>cnt;
    for (int i = 1;i<=cnt;i++)
    {
    
    
        cin>>n;
        m = n*n;
        for (int x = 0;x<m;x++)
            for (int y = 0;y<m;y++)
                cin>>a[x][y];
                
        if (check_row() &&check_col() && check_cell())
        {
    
    
            printf("Case #%d: Yes\n",i);
        }
        else
        {
    
    
            printf("Case #%d: No\n",i);
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_51955470/article/details/114055379