首页 > 代码库 > ACdream区域赛指导赛之手速赛系列(6)

ACdream区域赛指导赛之手速赛系列(6)

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 completed N2×N2 Sudoku matrix, your task is to determine whether it is a valid solution.

A valid solution must satisfy the following criteria:

  • Each row contains each number from 1 to N2, once each.
  • Each column contains each number from 1 to N2, once each.
  • Divide the N2×N2 matrix into N2 non-overlappingN×N sub-matrices. Each sub-matrix contains each number from 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).

T test cases follow. Each test case starts with an integer N(3 ≤ N ≤ 6).

The next N2 lines describe a completed Sudoku solution, with each line contains exactlyN2 integers.

All input integers are positive and less than 1000.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) andy is "Yes" (quotes for clarity only) if it is a valid solution, or "No" (quotes for clarity only) if it is invalid.

Sample Input

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>
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;   //这里的规律总结出来很重要!! 
    			cin>>a;
    			if(a<1||a>n*n){
                    flag=true;
                    continue;
                }
                // 对每行进行检查 
                if(row[i][a]){
                    flag=true;
                }
                else row[i][a]=true;
                // 对每列进行检查 
                if(col[j][a]){
                    flag=true;
                }
                else col[j][a]=true;
                // 对每个小的矩形块进行检查 
                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区域赛指导赛之手速赛系列(6)