博客
关于我
703. 数独检查 - AcWing题库
阅读量:629 次
发布时间:2019-03-14

本文共 1960 字,大约阅读时间需要 6 分钟。

数独是一种经典的单人数独游戏,目标是在一个9x9的矩阵中填入数字,使得每行、每列以及每个3x3的子矩阵都包含从1到9的数字。完成的数独矩阵需要满足以下条件:

  • 行检查:每行包含从1到N的所有数字,每个数字仅出现一次。
  • 列检查:每列包含从1到N的所有数字,每个数字仅出现一次。
  • 子矩阵检查:将整个矩阵划分为N个3x3的子矩阵,每个子矩阵包含从1到N的所有数字,每个数字仅出现一次。
  • 以下是实现数独有效性检查的步骤:

    1. 行检查

    遍历每一行,统计每个数字的出现情况,确保每个数字从1到N都出现且仅出现一次。

    2. 列检查

    类似行检查,遍历每一列,统计每个数字的出现情况,确保每个数字从1到N都出现且仅出现一次。

    3. 子矩阵检查

    将整个矩阵划分为N个3x3的子矩阵,检查每个子矩阵是否满足行和列的检查条件。

    4. 实现细节

    • 行检查:使用一个数组v来记录数字是否已出现,遍历每一行的每个数字,检查并标记。
    • 列检查:同样使用数组v,遍历每一列的每个数字,检查并标记。
    • 子矩阵检查:将矩阵划分为块,遍历每个子矩阵的每个数字,检查并标记。

    5. 代码实现

    #include 
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std; #define ll long long #define N 40 int a[N][N]; int v[N]; int m, n; bool check_row() { for (int i = 0; i < m; ++i) { memset(v, 0, sizeof(v)); for (int j = 0; j < m; ++j) { int num = a[i][j]; if (num < 1 || num > m) return false; if (!v[num]) { v[num] = 1; } else { return false; } } } return true; } bool check_col() { for (int i = 0; i < m; ++i) { memset(v, 0, sizeof(v)); for (int j = 0; j < m; ++j) { int num = a[j][i]; if (num < 1 || num > m) return false; if (!v[num]) { v[num] = 1; } else { return false; } } } return true; } bool check_k() { for (int i = 0; i < m; i += 3) { for (int j = 0; j < m; j += 3) { int dx = i; for (int x = 0; x < 3; ++x) { int xx = x + dx; if (xx >= m) continue; int dy = j; for (int y = 0; y < 3; ++y) { int yy = y + dy; if (yy >= m) continue; int num = a[xx][yy]; if (num < 1 || num > m) return false; if (!v[num]) { v[num] = 1; } else { return false; } } } } } return true; } int main() { int t; cin >> t; for (int case_num = 1; case_num <= t; ++case_num) { cin >> n; m = n * n; for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { cin >> a[i][j]; } } if (check_row() && check_col() && check_k()) { printf("Case #%d: Yes\n", case_num); } else { printf("Case #%d: No\n", case_num); } } return 0; }

    6. 输出结果

    每个测试用例输出一个结果,表示是否是有效的数独方案。如“Case #1: Yes”表示第一个测试用例是有效的数独方案,否则输出“No”。

    转载地址:http://jpxoz.baihongyu.com/

    你可能感兴趣的文章
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>