AcWing每日一题--数独检查

https://www.acwing.com/problem/content/705/

考虑每个独立方块的左上角和右下角。

单独检查每一个独立方块。

 1 #include<iostream>
 2 #include<unordered_map>
 3 using namespace std;
 4 const int N=40;
 5 int mp[N][N];
 6 bool check(int a,int b,int c,int d,int n){
 7     unordered_map<int,int> cnt;
 8     for(int i=a;i<=c;i++){
 9         for(int j=b;j<=d;j++){
10             cnt[mp[i][j]]++;
11         }
12     }
13     for(int i=1;i<=n;i++){
14         if(cnt[i]!=1){
15             return false;
16         }
17     }
18     return true;
19 }
20 int main(void){
21     int t;
22     cin>>t;
23     for(int k=1;k<=t;k++){
24         int n;
25         cin>>n;
26         for(int i=1;i<=n*n;i++){
27             for(int j=1;j<=n*n;j++){
28                 cin>>mp[i][j];
29             }
30         }
31         bool flag=true;
32         for(int i=1;i<=n*n;i++){
33             if(!check(i,1,i,n*n,n*n)||!check(1,i,n*n,i,n*n)){
34                 flag=false;
35             }
36         }
37         for(int i=1;i<=n;i++){
38             for(int j=1;j<=n;j++){
39                 if(!check( (i-1)*n+1 , (j-1)*n+1 , i*n , j*n , n*n)){
40                     flag=false;
41                 }
42             }
43         }
44         if(flag)
45             cout<<"Case #"<<k<<": Yes"<<endl;
46         else
47             cout<<"Case #"<<k<<": No"<<endl;
48     }
49     return 0;
50 }
原文地址:https://www.cnblogs.com/greenofyu/p/14361138.html