题解

205 条题解

  • 8
    @ 2018-02-08 22:23:40

    我的代码不一定是最好的,但一定是最容易看懂的 :) 各位,给个赞同吧

    
    #include <stdio.h>
    char verity(char data[81]){
        long right=0,right2=0;
        //验证横竖
        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                //i,j在此处既表示横行,又表示竖列
                right+=1<<data[i*9+j];
                right2+=1<<data[j*9+i];
            }
            //1<<1+1<<2+1<<3+...+1<<9=(1<<10)-2=1022.
            if(right!=(1<<10)-2||right2!=(1<<10)-2)
                return 0;
            right=right2=0;
        }
        right=0;
        //验证九宫格
        for(int a=0;a<3;a++){
            for(int b=0;b<3;b++){
                //a,b表示第a行第b列个九宫格
                //i,j是每个九宫格内的行列数
                for(int i=a*3;i<a*3+3;i++){
                    for(int j=b*3;j<b*3+3;j++){
                        right+=1<<data[i*9+j];
                    }
                    
                }
                if(right!=(1<<10)-2)
                        return 0;
                    right=0;
            }
        }
        return 1;
    
    }
    int main(int argc, char* argv[])
    {
        int N;
        scanf("%d",&N);
        //不用[N],我在本地调试时用的VC++不支持C99标准
        char data[20][81];
        //输入数据
        for(int j=0;j<N;j++){
            for(int i=0;i<81;i++){
                scanf("%d",&data[j][i]);
            }
            //验证
            if(verity(data[j]))
                printf("Right");
            else
                printf("Wrong");
            printf("\n");
        }
        return 0;
    }
    
    
    
  • 7
    @ 2017-09-20 16:16:21

    模拟

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int ALL=(1<<9)-1;
    int mp[9][9];
    bool chk_l(int x){
        int tmp=0;
        for ( int i=0; i<9; i++ ) tmp |= (1<<mp[x][i]);
        return tmp==ALL;
    }
    bool chk_r(int x){
        int tmp=0;
        for ( int i=0; i<9; i++ ) tmp |= (1<<mp[i][x]);
        return tmp==ALL;
    }
    bool chk_g(int x,int y){
        int tmp=0;
        for ( int i=x; i<=x+2; i++ )
            for ( int j=y; j<=y+2; j++ )
                tmp |= (1<<mp[i][j]);
        return tmp==ALL; 
    }
    bool chk(){
        for ( int i=0; i<9; i++ ) if( !chk_l(i) ) return false;
        for ( int i=0; i<9; i++ ) if( !chk_r(i) ) return false;
        for ( int i=0; i<9; i+=3)
            for ( int j=0; j<9; j+=3)
                if( !chk_g(i,j) ) return false;
        return true; 
    }
    int main(){
        int T;
        scanf("%d", &T);
        while( T-- ){
            for ( int i=0; i<9; i++ )
                for ( int j=0; j<9; j++ )
                    scanf("%d", &mp[i][j] ), mp[i][j]--;
            chk()?puts("Right"):puts("Wrong");
        } 
        return 0;
    }
    
  • 3
    @ 2018-07-18 15:11:35

    横向、竖向和九宫格是可以同时在一个循环里面检测的,只要有一个不满足即可以结束检测,因此可以极大的减少代码量

    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int arr[9][9];
    
    int main()
    {
        //freopen("input.txt", "r", stdin);
        
        int i, j, k;
        int n;
        scanf("%d", &n);
        
        int arr_idx[3][9];
        while(n--)
        {
            for(i = 0;i < 9; ++i)
            {
                for(j = 0; j < 9; ++j)
                {
                    scanf("%d", &arr[i][j]);
                }
            }
            
            //横竖向检测 
            int mapi[9] = {0, 0, 0, 3, 3, 3, 6, 6, 6};
            int mapj[9] = {0, 3, 6, 0, 3, 6, 0, 3, 6};
            
            for(i = 0; i < 9; ++i)
            {
                memset(arr_idx, 0, sizeof(arr_idx));
    
                for(j = 0; j < 9; ++j)
                {
                    if(++arr_idx[0][arr[i][j] - 1] > 1) break; //横向 
                    if(++arr_idx[1][arr[j][i] - 1] > 1) break; //竖向 
                    if(++arr_idx[2][arr[j / 3 + mapi[i]][j % 3 + mapj[i]] - 1] > 1) break; //九宫格 
                }
                if(j < 9) break;
            }
            if(i < 9)
            {
                printf("Wrong\n");
            }
            else
            {
                printf("Right\n");
            }    
        }
        
        //fclose(stdin);
        
        return 0;
    }
    
    • @ 2019-12-28 03:50:04

      考场上真的能想到这么绕的思路么... 这个说实话得看半天而且要是算法有错的话考场上不一定debug的出来...

  • 3
    @ 2017-12-08 21:19:37

    Ha

    var
     a:array[1..9,1..9]of longint;
     f:array[1..9]of boolean;
     i,j,k,l,m,n:longint;
     rw:boolean;
    begin
     readln(n);
     for i:=1 to n do
      begin
       rw:=true;
       for j:=1 to 9 do
        for k:=1 to 9 do
         read(a[j,k]);
       for j:=1 to 9 do
        begin
         fillchar(f,sizeof(f),false);
         for k:=1 to 9 do
          f[a[j,k]]:=true;
         for k:=1 to 9 do
          if f[k]=false then begin
                              writeln('Wrong');
                              rw:=false;
                              break;
                             end;
         if rw=false then break;
         fillchar(f,sizeof(f),false);
         for k:=1 to 9 do
          f[a[k,j]]:=true;
         for k:=1 to 9 do
          if f[k]=false then begin
                              writeln('Wrong');
                              rw:=false;
                              break;
                             end;
         if rw=false then break;
        end;
       if rw=false then continue;
       for j:=0 to 2 do
        begin
         for k:=0 to 2 do
          begin
           fillchar(f,sizeof(f),false);
           for l:=1 to 3 do
            for m:=1 to 3 do
             f[a[j*3+l,k*3+m]]:=true;
           for l:=1 to 9 do
            if f[l]=false then begin
                                writeln('Wrong');
                                rw:=false;
                                break;
                               end;
           if rw=false then break;
          end;
        if rw=false then break;
       end;
      if rw=false then continue;
      writeln('Right');
     end;
    end.
    
    
  • 3
    @ 2017-08-15 00:52:57
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    
    int main(){
        int i,n,a[10][10],j,k;
        scanf("%d",&n);
        for(k=1;k<=n;k++)
        {
            int e=1;
            for(i=1;i<=9;i++)
            {
                for(j=1;j<=9;j++)
                {
                    scanf("%d",&a[i][j]);
                }
            }
            if(k!=n)
                getchar();
            for(i=1;i<=9;i++)
            {
                int b[10]={0};
                    b[0]=1;
                for(j=1;j<=9;j++)
                {
                    if(b[a[i][j]]==0)
                        b[a[i][j]]=1;
                    else
                    {
                        printf("Wrong\n");
                        e=0;
                        break;
                    }
                }
                if(e==0)
                    break;
            }
            if(e==0)
                continue;
            for(j=1;j<=9;j++)
            {
                int b[10]={0};
                    b[0]=1;
                for(i=1;i<=9;i++)
                {
                    if(b[a[i][j]]==0)
                        b[a[i][j]]=1;
                    else
                    {
                        printf("Wrong\n");
                        e=0;
                        break;
                    }
                }
                if(e==0)
                    break;
            }
            if(e==0)
                continue;
            for(i=2;i<=8;i=i+3)
            {
                for(j=2;j<=8;j=j+3)
                {
                    int b[10]={0};
                    b[0]=1;
                    if(b[a[i][j]]==0)
                        b[a[i][j]]=1;
                    else
                        e=0;
                    if(b[a[i-1][j-1]]==0)
                        b[a[i-1][j-1]]=1;
                    else
                        e=0;
                    if(b[a[i][j-1]]==0)
                        b[a[i][j-1]]=1;
                    else
                        e=0;
                    if(b[a[i-1][j]]==0)
                        b[a[i-1][j]]=1;
                    else
                        e=0;
                    if(b[a[i+1][j]]==0)
                        b[a[i+1][j]]=1;
                    else
                        e=0;
                    if(b[a[i][j+1]]==0)
                        b[a[i][j+1]]=1;
                    else
                        e=0;
                    if(b[a[i+1][j+1]]==0)
                        b[a[i+1][j+1]]=1;
                    else
                        e=0;
                    if(b[a[i-1][j+1]]==0)
                        b[a[i-1][j+1]]=1;
                    else
                        e=0;
                    if(b[a[i+1][j-1]]==0)
                        b[a[i+1][j-1]]=1;
                    else
                        e=0;
                    if(e==0)
                        break;
                }
                if(e==0)
                break;
            }
            if(e!=0)
                printf("Right\n");
            else
                printf("Wrong\n");
        }
        return 0;
    }
    
  • 2
    @ 2020-02-06 11:40:52

    毫不夸张的说,我的代码思路清晰,也容易看懂(小骄傲)
    如果觉得好的话,点个赞吧。
    思路
    1.用#三维数组#实现多个数独的输入
    2.定义判断数独是否正确的函数Sudoku
    3.在这个判断函数中,要对 行 列 3×3矩阵 进行判断
    4.定义该判断函数Jcomplete判断1-9的的完整性
    下面给出代码:

    #include<iostream>
    using namespace std;
    bool Jcomplete(const int x[]) {//1-9判断
        for (int i = 1; i < 10; i++) {
            int j;
            for (j = 0; j < 9; j++)if (x[j] == i)break;
            if (j == 9)return false;
        }
        return true;
    }
    bool Sudoku(int x[][9]) {//数独判断
        int d = 1, y[9];
        for (int i = 0; i < 9; i++) {
            if (!Jcomplete(x[i]))return false;//行判断
            for (int j = 0; j < 9; j++)y[j] = x[i][j];
            if (!Jcomplete(y))return false;//列判断
            for (int j = 0; j < 9; j++)y[j] = x[i / 3 * 3 + j / 3][3 * i % 9 + j % 3];
            if (!Jcomplete(y))return false;//3*3判断
        }
        return true;
    }
    int main()
    {
        int N;
        int x[10][9][9];
        cin >> N;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < 9; j++)
                for (int k = 0; k < 9; k++)
                    cin >> x[N][j][k];
            if (Sudoku(x[N]))cout << "Right" << endl;
            else cout << "Wrong" << endl;
        }
        return 0;
    }
    
  • 2
    @ 2019-03-24 12:31:52

    不用写这么麻烦的1~9判断,只需要将9个数求和等于45,求积等于362880就可以确定是1~9了。

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int cr,cc;
        cin>>cc;
        for(cr=0;cr<cc;cr++)
        {
            bool flag=true;
            int sd[9][9];
            int i,j,k,l;
            int sum;
            long mu;
            for(i=0;i<9;i++)
            {
                for(j=0;j<9;j++)
                {
                    cin>>sd[i][j];
                }
            }
            for(i=0;i<9;i++)
            {
                sum=0;
                mu=1;
                for(j=0;j<9;j++)
                {
                    sum+=sd[i][j];
                    mu*=sd[i][j];
                }
                if(sum!=45||mu!=362880)
                {
                    flag=false;
                }
            }
            for(i=0;i<9;i++)
            {
                sum=0;
                mu=1;
                for(j=0;j<9;j++)
                {
                    sum+=sd[j][i];
                    mu*=sd[j][i];
                }
                if(sum!=45||mu!=362880)
                {
                    flag=false;
                }
            }
            for(i=0;i<9;i+=3)
            {
                for(j=0;j<9;j+=3)
                {
                    sum=0;
                    mu=1;
                    for(k=0;k<3;k++)
                    {
                        for(l=0;l<3;l++)
                        {
                            sum+=sd[i+k][j+l];
                            mu*=sd[i+k][j+l];
                        }
                    }
                    if(sum!=45||mu!=362880)
                    {
                        flag=false;
                    }
                }
            }
            if(flag)
            {
                cout<<"Right"<<endl;
            }
            else
            {
                cout<<"Wrong"<<endl;
            }
        }
        return 0;
    }
    
    
  • 2
    @ 2017-10-04 10:49:39

    函数中判断错误可增加输出。
    #include <iostream>
    using namespace std;

    int n;
    char a;
    int arr[9][9];
    int brr[24];

    int identify() {
    for (int j = 0; j < 9; j++) {
    int crr[10];
    for(int i=1;i<=9;i++){
    crr[i]=0;
    }
    for (int k = 0; k < 9; k++) {
    if (crr[arr[j][k]] == 0) {
    crr[arr[j][k]] = 1;
    } else {
    return 0;
    }
    }
    }
    //cout<<1<<endl;
    for (int k = 0; k < 9; k++) {
    int crr[10];
    for(int i=1;i<=9;i++){
    crr[i]=0;
    }
    for (int j = 0; j < 9; j++) {
    if (crr[arr[j][k]] == 0) {
    crr[arr[j][k]] = 1;
    } else {
    return 0;
    }
    }
    }
    //cout<<2<<endl;
    for (int j = 1; j <= 7; j += 3) {
    for (int k = 1; k <= 7; k += 3) {
    int crr[10];
    for(int i=1;i<=9;i++){
    crr[i]=0;
    }
    for (int x = j - 1; x <= j + 1; x++) {
    for (int y = j - 1; y <= j + 1; y++) {
    if (crr[arr[x][y]] == 0) {
    crr[arr[x][y]] = 1;
    } else {
    return 0;
    }
    }
    }
    }
    }
    return 1;
    }

    int main() {
    cin >> n;
    for (int i = 1; i < n; i++) {
    for (int j = 0; j < 9; j++) {
    for (int k = 0; k < 9; k++) {
    cin >> arr[j][k];
    }
    }
    brr[i] = identify();
    a = getchar();
    }
    for (int j = 0; j < 9; j++) {
    for (int k = 0; k < 9; k++) {
    cin >> arr[j][k];
    }
    }
    brr[n] = identify();
    for (int i = 1; i < n; i++) {
    if (brr[i] == 1) {
    cout << "Right" << endl;
    } else {
    cout << "Wrong" << endl;
    }
    }
    if (brr[n] == 1) {
    cout << "Right" ;
    } else {
    cout << "Wrong" ;
    }
    return 0;
    }

  • 1
    @ 2020-10-14 14:53:24
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    namespace dts
    {
        int n,a[9][9],vis[10];
        
        int check()
        {
            for (int i=0;i<9;i++)
            {
                memset(vis,0,sizeof(vis));
                for (int j=0;j<9;j++)
                    if (1<=a[i][j]&&a[i][j]<=9&&vis[a[i][j]]==0)
                        vis[a[i][j]]=1;
                    else
                        return 0;
            }
            for (int j=0;j<9;j++)
            {
                memset(vis,0,sizeof(vis));
                for (int i=0;i<9;i++)
                    if (1<=a[i][j]&&a[i][j]<=9&&vis[a[i][j]]==0)
                        vis[a[i][j]]=1;
                    else
                        return 0;
            }
            for (int i=0;i<9;i+=3)
                for (int j=0;j<9;j+=3)
                {
                    memset(vis,0,sizeof(vis));
                    for (int k=i;k<=i+2;k++)
                        for (int l=j;l<=j+2;l++)
                            if (1<=a[k][l]&&a[k][l]<=9&&vis[a[k][l]]==0)
                                vis[a[k][l]]=1;
                            else
                                return 0;
                }
            return 1;
        }
        
        void main()
        {
            scanf("%d",&n);
            for (int rp=1;rp<=n;rp++)
            {
                for (int i=0;i<9;i++)
                    for (int j=0;j<9;j++)
                        scanf("%d",&a[i][j]);
                if (check())
                    printf("Right\n");
                else
                    printf("Wrong\n");
            }
        }
    }
    
    int main()
    {
        dts::main();
    }
    
  • 1
    @ 2019-12-28 03:57:08

    算法竞赛入门经典里有句话叫 Keep It Simple and Stupid(KISS),我觉得特别适合这道题。
    考场上最容易想到且最好实现的算法才是最靠谱的。这题思路就是每次把9个数加到vector里,然后用标准库里的find函数看是不是1-9都出现了一遍,这样可以少写很多代码而且这个思路写出来也不容易出bug。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int arr[9][9];
    vector<int> v;
    
    // 检查vector里是不是1-9九个数都有
    bool check_vector() {
        for (int i = 1; i <= 9; ++i) {
            if (find(v.begin(), v.end(), i) == v.end()) {
                return 0;
            }
        }
        return 1;
    }
    
    bool check() {
        // 检查行
        for (int i = 0; i < 9; ++i) {
            v.clear();
            for (int j = 0; j < 9; ++j) {
                v.push_back(arr[i][j]);
            }
            if (!check_vector())
                return 0;
        }
        // 检查列
        for (int i = 0; i < 9; ++i) {
            v.clear();
            for (int j = 0; j < 9; ++j) {
                v.push_back(arr[j][i]);
            }
            if (!check_vector())
                return 0;
        }
        // 检查方格
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                v.clear();
                for (int k = 0; k < 3; ++k) {
                    for (int p = 0; p < 3; ++p) {
                        v.push_back(arr[3 * i + k][3 * j + p]);
                    }
                }
                if (!check_vector())
                    return 0;
            }
        }
        return 1;
    }
    
    int main(int argc, char *argv[]) {
    #ifdef DEBUG
        freopen("a.in", "r", stdin);
    #endif
        int n;
        cin >> n;
    
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < 9; ++j) {
                for (int k = 0; k < 9; ++k) {
                    cin >> arr[j][k];
                }
            }
            if (check())
                cout << "Right" << endl;
            else
                cout << "Wrong" << endl;
        }
    
        return 0;
    }
    
    
  • 1
    @ 2019-04-05 21:51:51
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<ctime>
    #include<algorithm>
    #include<iomanip>
    #define f(i, a, b) for(int i = a; i <=  b; i++)
    using namespace std;
    const int N = 15;
    int a[N][N];
    int l[N][N] = 
    {
        {0,0,0,0,0,0,0,0,0,0},
        {0,1,1,1,2,2,2,3,3,3},
        {0,1,1,1,2,2,2,3,3,3},
        {0,1,1,1,2,2,2,3,3,3},
        {0,4,4,4,5,5,5,6,6,6},
        {0,4,4,4,5,5,5,6,6,6},
        {0,4,4,4,5,5,5,6,6,6},
        {0,7,7,7,8,8,8,9,9,9},
        {0,7,7,7,8,8,8,9,9,9},
        {0,7,7,7,8,8,8,9,9,9}
    };
    bool m1[N][N], m2[N][N], m3[N][N];
    int t;
    int main()
    {
        int m;bool sd;
        scanf("%d", &t);
        while(t--){
            sd = true;
            f(i, 1, 9){
                f(j, 1, 9){
                    scanf("%d", &m);
                    if(m1[i][m] || m2[j][m] || m3[l[i][j]][m]){
                        sd = false;
                        break;
                    }
                    m1[i][m] = m2[j][m] = m3[l[i][j]][m] = true;
                }
                if(!sd)break;
            }
            if(!sd)printf("Wrong\n");
            else printf("Right\n");
        }
        
        return 0;
    }
    **自己拿去调试**
    
  • 1
    @ 2019-03-16 22:53:56

    简单模拟

    #include <iostream>
    using namespace std;
    
    int i, j, k;
    int yans[10];//模拟标记,记录1~9的数字并验证
                             //可以多开几个标记数组共同验证,达到减少复杂度的目的
    
    bool toyan() {
      int k = 1, flag = 1;
      for (k = 1; k < 10; k++) {
        if (yans[k] != 1)
          flag = 0;
        yans[k] = 0; //清除标记
      }
      if (flag)
        return true;
      else
        return false;
    }
    
    bool yanzheng(int a[9][9]) {
      for (i = 0; i < 9; i++) {
        //验证列
        for (j = 0; j < 9; j++) {
          yans[a[j][i]] = 1;
        }
        if (!toyan())
          return false;
        //验证行
        for (j = 0; j < 9; j++) {
          yans[a[i][j]] = 1;
        }
        if (!toyan())
          return false;
      }
    
      //验证3*3方块
      for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
          for (int m = 3 * i; m < 3 * i + 3; m++) {
            for (int n = 3 * j; n < 3 * j + 3; n++) {
              yans[a[m][n]] = 1;
            }
          }
          if (!toyan())
            return false;
        }
      }
      return true;
    }
    
    int main() {
      int a[9][9];
      int n;
      cin >> n;
      for (int i = 0; i < n; i++) {
        for (j = 0; j < 81; j++) {
          cin >> a[j / 9][j % 9];
        }
        getchar();
        if (yanzheng(a))
          cout << "Right" << endl;
        else
          cout << "Wrong" << endl;
      }
      getchar();
    }
    
  • 1
    @ 2019-02-14 21:08:04

    #include<iostream>
    using namespace std;
    int a[100][10][10];
    int main()
    {
    int n,x,y,z=0;
    cin>>n;
    for(int i=0;i<n;i++)
    for(int j=0;j<9;j++)
    for(int k=0;k<9;k++)
    cin>>a[i][j][k];
    for(int i=0;i<n;i++)
    for(int j=0;j<9;j++)
    {
    for(int k=0;k<9;k++)
    {
    for(int m=k+1;m<9;m++)
    if((a[i][j][k]==a[i][j][m])||(a[i][j][k]==a[i][m][k]))
    {
    z=1;
    break;
    }
    int e=(j/3)*3+1;
    int f=(k/3)*3+1;
    for(int c=e-1;c<=e+1;c++)
    {
    for(int d=f-1;d<=f+1;d++)
    if((a[i][j][k]==a[i][c][d])&&((j!=c)&&(k!=d)))
    {
    z=1;
    break;
    }
    if(z==1) break;
    }
    if(z==1) break;
    }
    if(z==1)
    {
    z=0;
    cout<<"Wrong"<<endl;
    break;
    }
    else
    {
    z=0;
    cout<<"Right"<<endl;
    break;
    }
    }
    return 0;
    }

  • 1
    @ 2018-12-20 17:05:29

    #include<bits/stdc++.h>
    using namespace std;
    bool check(int a[10][10])
    {
    int b[10]={0};
    int cc=4;
    for(int x=0;x<9;x++)
    {
    for(int y=0;y<9;y++)
    {
    switch(a[x][y])
    {
    case 1:b[1]+=1;break;
    case 2:b[2]+=1;break;
    case 3:b[3]+=1;break;
    case 4:b[4]+=1;break;
    case 5:b[5]+=1;break;
    case 6:b[6]+=1;break;
    case 7:b[7]+=1;break;
    case 8:b[8]+=1;break;
    case 9:b[9]+=1;break;

    }
    }
    for(int t=1;t<=9;t++)
    if(b[t]!=x+1)
    cc= 0;
    }
    memset(b,0,sizeof(b));
    for(int y=0;y<9;y++)
    {
    for(int x=0;x<9;x++)
    {
    switch(a[x][y])
    {
    case 1:b[1]+=1;break;
    case 2:b[2]+=1;break;
    case 3:b[3]+=1;break;
    case 4:b[4]+=1;break;
    case 5:b[5]+=1;break;
    case 6:b[6]+=1;break;
    case 7:b[7]+=1;break;
    case 8:b[8]+=1;break;
    case 9:b[9]+=1;break;
    }
    }
    for(int t=1;t<=9;t++)
    if(b[t]!=y+1)
    cc= 0;
    }
    int sum=0;
    for(int x=0;x<9;x++)
    {
    for(int y=0;y<9;y++)
    sum = sum+a[x][y];
    if(sum!=45)
    cc= 0;
    sum =0;
    }
    for(int y=0;y<9;y++)
    {
    for(int x=0;x<9;x++)
    sum = sum+a[x][y];
    if(sum!=45)
    cc= 0;
    sum=0;
    }
    sum=0;
    for(int x=0;x<3;x++)
    {
    for(int y=0;y<3;y++)
    sum=sum+a[x][y];
    }
    if(sum!=45)
    cc=0;
    if(cc!=0)
    cc=1;
    return cc;
    }
    int main()
    { int n;
    int a[10][10];
    cin>>n;
    for(int t=0;t<n;t++)
    {

    for(int i=0;i<9;i++)
    {
    for(int j=0;j<9;j++)
    {
    cin>>a[i][j];
    }
    }
    if(check(a)==0)
    cout<<"Wrong"<<endl;
    else
    cout<<"Right"<<endl;
    memset(a,0,sizeof(a));
    }
    return 0;
    }

  • 1
    @ 2018-02-27 15:55:21
    #include<bits/stdc++.h>
    void judge(int n,int a[][10][10]){
        bool flag[10]={0};
        for(int i=1;i<=9;++i){
            for(int j=1;j<=9;++j) if(1^flag[a[n][i][j]]) flag[a[n][i][j]]=1;else{printf("Wrong\n");return;}
            memset(flag,0,sizeof(flag));
        }
        for(int i=1;i<=9;++i){
            for(int j=1;j<=9;++j) if(1^flag[a[n][i][j]]) flag[a[n][i][j]]=1;else{printf("Wrong\n");return;}
            memset(flag,0,sizeof(flag));
        }
        for(int i=0;i<=6;i=i+3)
            for(int j=0;j<=6;j=j+3){
                for(int k=1;k<=3;++k)
                    for(int l=1;l<=3;++l)
                        if(1^flag[a[n][i+k][j+l]]) flag[a[n][i+k][j+l]]=1;else{printf("Wrong\n");return;}
                memset(flag,0,sizeof(flag));
            }
        printf("Right\n");
    }
    int main(){
        int n,a[21][10][10];
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            for(int j=1;j<=9;++j)
                for(int k=1;k<=9;++k)
                    scanf("%d",&a[i][j][k]);
        for(int i=1;i<=n;++i) judge(i,a);
    }
    
  • 1
    @ 2017-11-30 23:08:33
    /*Tokgo*/
    /*
    没什么技巧
    纯模拟
    当然可以用一维数组
    舒服一点
    */
    #include <iostream>
    #include <vector>
    #include <cstring>
    using namespace std;
    int m[82];
    bool z[9][10],h[9][10];
    bool k[3][3][10];
    int zj[9],hj[9],kj[3][3];
    int main(){
        int n;
        cin>>n;
        while(n--){
            bool jud=1;
            memset(z,0,sizeof(z));
            memset(h,0,sizeof(h));
            memset(k,0,sizeof(k));
            memset(zj,0,sizeof(k));
            memset(hj,0,sizeof(k));
            memset(kj,0,sizeof(k));
            for(int i=1;i<=81;++i){
                cin>>m[i];      
                if(!z[i%9][m[i]]){
                    z[i%9][m[i]]=1;
                    zj[i%9]++;
                }
                else jud=0;
                if(!h[(i-1)/9][m[i]]){
                    h[(i-1)/9][m[i]]=1;
                    ++hj[(i-1)/9];
                }
                else jud=0;
                if(!k[(((i-1)%9))/3][((i-1)/9)/3][m[i]]){
                    k[(((i-1)%9))/3][((i-1)/9)/3][m[i]]=1;
                    ++kj[(((i-1)%9))/3][((i-1)/9)/3];
                }
                else jud=0;
            }
            for(int i=0;i<9;++i){
                if(zj[i]!=9||hj[i]!=9)
                    jud=0;
            }
            for(int i=0;i<3;++i){
                for(int j=0;j<3;++j){
                    if(kj[i][j]!=9)
                        jud=0;
                }
            }
            if(jud)
                cout<<"Right"<<endl;
            else
                cout<<"Wrong"<<endl;
        }
        return 0;
    }
    
  • 0
    @ 2022-08-08 20:57:32

    python3代码,略显简陋但按部就班!:-)

    def judge(s):
        flag = False
        for i in range(0,9,3):
            a,b,c = [],[],[]
            for j in range(i,i+3):
                a.extend(s[j][0:3])
                b.extend(s[j][3:6])
                c.extend(s[j][6:9])
            if len(set(a))+len(set(b))+len(set(c)) != 27:
                flag = True
                break
        return flag
            
    n = int(input())
    for i in range (n):
        s = [];flag = False
        s.append(list(map(int,input().split())))
        if not s[0]: # 空行
            s[0] = (list(map(int,input().split())))
        for j in range(8):
            s.append(list(map(int,input().split())))
        s2 = list(map(list, zip(*s))) # 利用解包 矩阵转置
        for j in range(9): # 行列检验
            if len(set(s[i]))+len(set(s2[i])) != 18:
                flag = True
                break
        #九宫格检验
        flag = flag or judge(s)
        print(["Right","Wrong"][flag])
        
    
    
  • 0
    @ 2021-10-21 20:26:00

    我觉得我的最简单暴力,但是代码比较多,复杂度比较高些

    #include<bits/stdc++.h>
    using namespace std;
    int maps[10][10];
    bool used[10];
    bool chackused() {
        for (int i = 1; i < 10; i++)
        {
            if (!used[i]) return false;
        }
        return true;
    }
    bool chackrow() {
        for (int i = 1; i < 10 ; i++)
        {
            memset(used, false, sizeof(used));
            for (int j = 1; j < 10; j++)
            {
                used[maps[i][j]] = true;
            }
            if (!chackused()) return false;
        }
        return true;
    }
    bool chackcol() {
        for (int j = 1; j < 10; j++){
            memset(used, false, sizeof(used));
            for (int i = 1; i < 10; i++)
            {
                used[maps[i][j]] = true;
            }
            if (!chackused()) return false;
        }
        return true;
    }
    bool chack33(int ai,int bj) {
        memset(used, false, sizeof(used));
        for (int i = ai; i < ai+3 ; i++)
        {
            for (int j = bj; j < bj+3; j++)
            {
                used[maps[i][j]] = true;
            }
        }
        return chackused();
    }
    bool chack99() {
        for (int i = 1; i < 10; i+=3)
        {
            for (int j = 1; j < 10 ; j+=3)
            {
                if (!chack33(i, j)) return false;
            }
        }
        return true;
    }
    int main() {
        int n;
        cin >> n;
        while (n--)
        {
            for (int i = 1; i < 10; i++)
            {
                for (int j = 1; j < 10; j++)
                {
                    cin >> maps[i][j];
                }
            }
            if (chackrow() && chackcol() && chack99()) cout << "Right" << endl;
            else cout << "Wrong" << endl;
        }
        return 0;
    }
    
  • 0
    @ 2021-06-04 10:53:35

    无非是测试横竖以及9个方格内是否出现了全部的1~9。那么如何验证是否出现了全部的1~9呢?可以用乘法。9的阶乘是362880,如果相乘的不是这9个数字,那么乘积一定不是362880。
    ```cpp
    #include <iostream>
    using namespace std;

    int main()
    {
    int n;
    cin >> n;
    int a[9][9];
    bool iswrong;
    while (n--)
    {
    iswrong = false;
    for (int i = 0; i < 9; i++)
    {
    for (int j = 0; j < 9; j++)
    cin >> a[i][j];
    }
    for (int i = 0; i < 9; i++)
    {
    if (a[i][0] * a[i][1] * a[i][2] * a[i][3] * a[i][4] * a[i][5] * a[i][6] * a[i][7] * a[i][8] != 362880)
    {
    cout << "Wrong" << endl;
    iswrong = true;
    break;
    }
    if (a[0][i] * a[1][i] * a[2][i] * a[3][i] * a[4][i] * a[5][i] * a[6][i] * a[7][i] * a[8][i] != 362880)
    {
    cout << "Wrong" << endl;
    iswrong = true;
    break;
    }
    }
    if (!iswrong)
    {
    int multi;
    for (int i = 0; i < 9; i += 3)
    {
    multi = 1;
    for (int j = 0; j < 3; j++)
    {
    for (int k = 0; k < 3; k++)
    multi *= a[i + j][i + k];
    }
    if (multi != 362880)
    {
    cout << "Wrong" << endl;
    iswrong = true;
    break;
    }
    }
    }
    if (!iswrong)
    cout << "Right" << endl;
    }
    return 0;
    }
    ```

  • 0
    @ 2019-09-11 23:34:20

    //数独验证
    #include<iostream>
    using namespace std;
    #include<algorithm>
    #include<stdlib.h>
    #include<string.h>

    int matrix[15][15] = { 0 };
    int n;
    bool row_check() {
    int flag[15] = { 0 };
    for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
    if (flag[matrix[i][j]] >= 2)
    return false;
    flag[matrix[i][j]]++;
    }
    memset(flag, 0, sizeof(flag));
    }
    return true;
    }

    bool column_check() {
    int flag[15] = { 0 };
    for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
    if (flag[matrix[j][i]] >= 2)
    return false;
    flag[matrix[j][i]]++;
    }
    memset(flag, 0, sizeof(flag));
    }
    return true;
    }

    bool block_check() {
    int flag[15] = { 0 };
    for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
    for (int k = 0; k < 3; k++) {
    for (int l = 0; l < 3; l++) {
    flag[matrix[i * 3 + k][j * 3 + l]]++;
    }
    }
    for(int k = 1; k <= 9; k++)
    if (!flag[k])
    return false;
    memset(flag, 0, sizeof(flag));
    }
    }
    return true;
    }

    int main()
    {
    cin >> n;
    while (n--) {
    for (int i = 0; i < 9; i++)
    for (int j = 0; j < 9; j++)
    cin >> matrix[i][j];
    getchar();
    if (row_check() && column_check() && block_check())
    cout << "Right" << endl;
    else cout << "Wrong" << endl;
    }
    return 0;
    }

    这题有坑 正常人谁数独会填0的

信息

ID
1335
难度
4
分类
模拟 点击显示
标签
递交数
5140
已通过
1985
通过率
39%
被复制
20
上传者