Acwing 703.数独检查

在这里插入图片描述
在这里插入图片描述
输入样例:

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

输出样例:

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

这道题好水,直接代码吧。

#include<iostream>
#include<cstring>
using namespace std;
const int N=7;
int t;
int g[N*N][N*N];
int h[N*N][N*N],l[N*N][N*N],jg[N][N][N*N];
bool check()
{
    
    
    for(int i=0;i<t*t;i++)
    for(int j=0;j<t*t;j++)
    {
    
    
        if(h[i][g[i][j]]||l[j][g[i][j]]||jg[i/t][j/t][g[i][j]]||g[i][j]>t*t)
        return false;
        h[i][g[i][j]]=l[j][g[i][j]]=jg[i/t][j/t][g[i][j]]=1;
    }
}
int main(void)
{
    
    
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    
    
        cin>>t;
        memset(h,0,sizeof(h));
        memset(l,0,sizeof(h));
        memset(jg,0,sizeof(jg));
        for(int i=0;i<t*t;i++)
        for(int j=0;j<t*t;j++)
        cin>>g[i][j];
        printf("Case #%d: ",i);
        if(check())
        cout<<"Yes"<<endl;
        else
        cout<<"No"<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_52358098/article/details/113582248