day25 703 Sudoku check (simulation)

703. Sudoku Check

Sudoku is a popular single player game.

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

Each 9x9matrix will have some numbers already given at the beginning of the game, usually with a unique solution.

Insert picture description here
Insert picture description here

Given completed N 2 ∗ N 2 N^2∗N^2N2N2 Sudoku matrix, your task is to determine whether it is an effective solution.

An effective solution must meet the following conditions:

Each row contains from 1 to 11 toN 2 N^2NEach digit of 2 , once for each digit.
Each column contains from1 to 11 toN 2 N^2NEach digit of 2 , once for each digit.
SetN 2 ∗ N 2 N^2∗N^2N2N2 The matrix is ​​divided intoN 2 N^2N2 non-overlappingN ∗ NN∗NN N submatrix. Each sub-matrix contains from1 to 11 toN 2 N^2NEach digit of 2 , once for each digit.
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 the integer TTT , which means there is a total ofTTT group test data.

The first row of each group of data contains integer NNN

Next N 2 N^2N2 lines, each line containsN 2 N^2N2 digits (neither exceed1000 10001 0 0 0 ), 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 xis the group number (from 1 11 start), if the given matrix is ​​a valid solution thenyyesYes, otherwiseyyesNo.

Data range
1 ≤ T ≤ 100, 1≤T ≤100,1T100,
3 ≤ N ≤ 6 3≤N≤6 3N6
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

Ideas:

Using the hashSetpresence or absence of data, each test rows, columns, the number of small blocks of data to meet the requirements of a range
check is: each row, column, all the numbers stored in the small block hashSet, if hashSetthe size < n*nis not met
across the border inspection

Java code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;

public class Main {
    
    
    static int[][] a = new int[40][40];
    public static void main(String[] args) throws IOException {
    
    
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(reader.readLine());
        int s = 1;
        while (s <= t){
    
    
            int n = Integer.parseInt(reader.readLine());
            int m = n*n;
            for (int i = 1; i <= m; i++) {
    
    
                String[] str = reader.readLine().split(" ");
                for (int j = 1; j <= m; j++) {
    
    
                    a[i][j] = Integer.parseInt(str[j - 1]);
                }
            }
            if(check_row(a, n) && check_col(a, n) && check_square(a, n)){
    
    
                System.out.println("Case #" + s + ": Yes");
            }else {
    
    System.out.println("Case #" + s + ": No");}
            s ++;
        }


    }

    private static boolean check_square(int[][] a, int n) {
    
    
        int m = n*n;
        for (int i = 1; i <= m; i += n) {
    
    
            for (int j = 1; j <= m; j+= n) {
    
    
                HashSet<Integer> hashSet = new HashSet<Integer>();
                for (int k = i; k <= i + n; k++) {
    
    
                    for (int l = j; l <= j + n; l++) {
    
    
                        hashSet.add(a[k][l]);
                    }
                }
                if (hashSet.size() < m) return false;
            }
        }
        return true;
    }

    private static boolean check_col(int[][] a, int n) {
    
    
        int m = n*n;
        for (int i = 1; i <= m; i++) {
    
    
            HashSet<Integer> hashSet = new HashSet<Integer>();
            for (int j = 1; j <= m; j++) {
    
    
                if(a[i][j] < 1 || a[i][j] > m) return false;
                hashSet.add(a[i][j]);
            }
            if (hashSet.size() < m) return false;
        }
        return true;
    }

    private static boolean check_row(int[][] a, int n) {
    
    
        int m = n*n;
        for (int i = 1; i <= m; i++) {
    
    
            HashSet<Integer> hashSet = new HashSet<Integer>();
            for (int j = 1; j <= m; j++) {
    
    
                if(a[i][j] < 1 || a[i][j] > m) return false;
                hashSet.add(a[j][i]);
            }
            if (hashSet.size() < m) return false;
        }
        return true;
    }

}

Insert picture description here

Guess you like

Origin blog.csdn.net/YouMing_Li/article/details/113922983