• ACdream 1195 Sudoku Checker (数独)


    Sudoku Checker
    Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu

    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-overlapping N×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 exactly N2 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) and y 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>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    const int maxn=7*7;
    int a[maxn][maxn];
    bool vis[1005];//其实不用开这么大 超过n*n直接返回0了 开40就行 开大点保险
    bool check(int n) //这里如果想不传参可以定义为全局变量
    {
        for(int i=1; i<=n*n; i++)
        {
            memset(vis,0,sizeof(vis));//注意初始化该放的位置
            for(int j=1; j<=n*n; j++)
            {
                if(a[i][j]<1||a[i][j]>n*n) return 0;//注意越界
                if(vis[a[i][j]]) return 0;
                vis[a[i][j]]=1;
            }
        }
        for(int i=1; i<=n*n; i++)
        {
            memset(vis,0,sizeof(vis));
            for(int j=1; j<=n*n; j++)
            {
                if(a[j][i]<1||a[j][i]>n*n) return 0;//这里第一次直接复制的ij写反了 但是也过了 汗
                if(vis[a[j][i]]) return 0;
                vis[a[j][i]]=1;
            }
        }
        for(int si=0; si<n*n; si+=n)
            for(int sj=0; sj<n*n; sj+=n)
            {
                memset(vis,0,sizeof(vis));
                for(int i=1; i<=n; i++)
                {
                    for(int j=1; j<=n; j++)
                    {
                        int x=si+i;
                        int y=sj+j;
                        if(a[x][y]<1||a[x][y]>n*n) return 0;
                        if(vis[a[x][y]]) return 0;//注意vis是一维的
                        vis[a[x][y]]=1;
                    }
                }
            }
        return 1;//默认也是返回1
    }
    int main()
    {
        int t,cas=1,n;
        cin>>t;
        while(t--)
        {
            cin>>n;
            for(int i=1; i<=n*n; i++)
                for(int j=1; j<=n*n; j++)
                    cin>>a[i][j]; //也可以在这里判断一下如果越界直接No
            if(check(n)) printf("Case #%d: Yes
    ",cas++);//传n
            else printf("Case #%d: No
    ",cas++);
        }
        return 0;
    }
    
    
    
     
  • 相关阅读:
    图文详解——将本地项目上传到码云(gitee)远程仓库
    mysql 查询正在执行的sql
    MySql 缓冲池(buffer pool) 和 写缓存(change buffer) 转
    SQLserver 查询某个表的字段及字段属性
    FastDFS 客户端(二)
    FastDFS 服务器(一)
    C# WPF 正常的项目突然提示 当前上下文中不存在名称“InitializeComponent”
    C# 程序自动重启的解决方法
    Array知识点总结(一)
    JavaScript中为false的情况归档
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5711976.html
Copyright © 2020-2023  润新知