题解

205 条题解

  • 0
    @ 2019-07-27 08:36:46

    #include<bits/stdc++.h>
    using namespace std;
    bool sd(int x[10][10])
    {
    bool b[100];
    memset(b,false,sizeof(b));
    for(int i=1;i<=9;i++)
    {
    for(int j=1;j<=9;j++)
    {
    b[x[i][j]]=true;
    }
    for(int r=1;r<=9;r++)
    {
    if(b[r]==false)
    {
    return false;
    }
    }
    }
    memset(b,false,sizeof(b));
    for(int i=1;i<=9;i++)
    {
    for(int j=1;j<=9;j++)
    {
    b[x[j][i]]=true;
    }
    for(int r=1;r<=9;r++)
    {
    if(b[r]==false)
    {
    return false;
    }
    }
    }
    memset(b,false,sizeof(b));
    for(int k=1;k<=7;k+=3)
    {
    for(int r=1;r<=7;r+=3)
    {
    for(int i=k;i<=k+2;i++)
    {
    for(int j=r;j<=r+2;j++)
    {
    b[x[i][j]]=true;
    }
    }
    for(int q=1;q<=9;q++)
    {
    if(b[q]==false)
    {
    return false;
    }
    }
    }
    }
    return true;

    }
    int a[10][10],n;
    char ch;
    int main()
    {
    cin>>n;
    for(int k=1;k<=n;k++)
    {
    for(int i=1;i<=9;i++)
    {
    for(int j=1;j<=9;j++)
    {
    cin>>a[i][j];
    }
    }
    if(sd(a))
    {
    cout<<"Right"<<endl;
    }
    else
    {
    cout<<"Wrong"<<endl;
    }
    cin.get(ch);
    }
    }

  • 0
    @ 2019-07-25 20:44:45

    简洁
    #include <iostream>
    using namespace std;
    bool jgg(int a[9][9],int x,int y)
    {
    bool b[9]={false,false,false,false,false,false,false,false,false},c=true;
    b[a[x][y]-1]=true;b[a[x][y+1]-1]=true;b[a[x][y+2]-1]=true;
    b[a[x+1][y]-1]=true;b[a[x+1][y+1]-1]=true;b[a[x+1][y+2]-1]=true;
    b[a[x+2][y]-1]=true;b[a[x+2][y+1]-1]=true;b[a[x+2][y+2]-1]=true;
    for(int i;i<9;i++) if(b[i]==false) {c=false;break;}
    return c;
    }
    int main()
    {
    int a[9][9],i,j,k,n;
    bool b[9],RW;
    cin>>n;
    for(i=0;i<n;i++)
    {
    RW=true;
    for(j=0;j<9;j++) for(k=0;k<9;k++)
    cin>>a[j][k];
    for(j=0;j<9;j++) b[j]=false;
    for(j=0;j<9;j++)
    {
    for(k=0;k<9;k++) b[a[j][k]-1]=true;
    for(k=0;k<9;k++) if(b[k]==false) RW=false;
    for(k=0;k<9;k++) b[k]=false;
    if(RW==false) {cout<<"Wrong\n";break;}
    }
    for(j=0;j<9;j++)
    {
    if(RW==false) break;
    for(k=0;k<9;k++) b[a[k][j]-1]=true;
    for(k=0;k<9;k++) if(b[k]==false) RW=false;
    for(k=0;k<9;k++) b[k]=false;
    if(RW==false) {cout<<"Wrong\n";break;}
    }
    if(RW) for(j=0;j<3;j++) if(RW) for(k=0;k<3;k++) if(jgg(a,j*3,k*3)==false) {RW=false;cout<<"Wrong\n";break;}
    if(RW) cout<<"Right\n";
    }
    return 0;
    }

  • 0
    @ 2019-07-25 20:12:38

    C++
    九宫格是最困难的一环,我们用一个布尔型函数fang(int nine[9][9],int x,int y),其中第一个数组是先把数独弄进去,然后x,y是九宫格左上角坐标,函数中设置两个数组[9],一个用来存放九宫格中的九个数字,另一个记录123456789分别出现的次数,然后检测是否有数字出现次数不为1,则这不是正确的数独

    #include <iostream>
    using namespace std;
    bool shu(int nine[9][9],int a)
    {
        bool ok=true;int i,n[9]={0,0,0,0,0,0,0,0,0};
        for(i=0;i<9;i++) n[nine[i][a]-1]++;
        for(i=0;i<9;i++) if(n[i]!=1) {ok=false;break;}
        return ok;
    }
    bool heng(int nine[9][9],int a)
    {
        bool ok=true;int i,n[9]={0,0,0,0,0,0,0,0,0};
        for(i=0;i<9;i++) n[nine[a][i]-1]++;
        for(i=0;i<9;i++) if(n[i]!=1) {ok=false;break;}
        return ok;
    }
    bool fang(int nine[9][9],int x,int y)
    {
        bool ok=true;int i,n[9]={0,0,0,0,0,0,0,0,0},a[9];
        a[0]=nine[x][y];a[1]=nine[x][y+1];a[2]=nine[x][y+2];
        a[3]=nine[x+1][y];a[4]=nine[x+1][y+1];a[5]=nine[x+1][y+2];
        a[6]=nine[x+2][y];a[7]=nine[x+2][y+1];a[8]=nine[x+2][y+2];
        for(i=0;i<9;i++) n[a[i]-1]++;
        for(i=0;i<9;i++) if(n[i]!=1) {ok=false;break;}
        return ok;
    }
    int main()
    {
        int n,nine[9][9],i,j,k;bool c;
        cin>>n;
        for(i=0;i<n;i++) 
        {
            c=true;
            for(j=0;j<9;j++) for(k=0;k<9;k++) cin>>nine[j][k];
            for(j=0;j<9;j++)
            {
                if(heng(nine,i)==false) {cout<<"Wrong"<<endl;c=false;break;}else
                if(shu(nine,i)==false) {cout<<"Wrong"<<endl;c=false;break;}
            }
            if(c) for(j=0;j<=6;j+=3) if(c) for(k=0;k<=6;k+=3) if(fang(nine,j,k)==false) {cout<<"Wrong"<<endl;c=false;break;}
            if(c) cout<<"Right"<<endl;
        }
        return 0;
    }
    
  • 0
    @ 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;
    }

  • 0
    @ 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;
    }

  • 0
    @ 2019-01-15 16:20:51

    //验证是不是九宫格
    #include<iostream>
    using namespace std;
    bool panduan(int (*a)[10]);
    int main()
    {
    int Array[10][10];
    int group;
    cin>>group;
    while(group--)
    {
    for(int j=1;j<=9;j++)
    {//初始化数组也就是九宫格
    for(int u=1;u<=9;u++)
    {
    cin>>Array[j][u];
    }
    }
    cout<<endl;
    if(panduan(Array))
    {
    cout<<"Right"<<endl;
    }
    else
    {
    cout<<"Wrong"<<endl;
    }
    }
    return 0;
    }
    bool panduan(int (*a)[10])
    {
    int book[10];
    //先判断行
    for(int i=1;i<=9;i++)
    {
    int sum=0;
    for(int j=1;j<=9;j++)
    {//每一行的每一个数字出现再1--9中没
    book[a[i][j]]=1;
    }
    for(int q=1;q<=9;q++)
    {
    sum+=book[q];
    }
    for(int w=1;w<=9;w++)
    {
    book[w]=0;
    }
    if(sum!=9)
    {
    return false;
    }
    }//比列
    for(int o=1;o<=9;o++)
    {
    int sum1=0;
    for(int zz=1;zz<=9;zz++){
    book[a[zz][o]]=1;
    }
    for(int rr=1;rr<=9;rr++)
    {
    sum1+=book[rr];
    }
    for(int p=1;p<=9;p++)
    {//变成0
    book[p]=0;
    }
    if(sum1!=9)
    {
    return false;
    }
    }

    return true;
    }
    为什么我的错的? 我明明就对的啊

  • 0
    @ 2018-12-19 15:39:28
    #include<stdio.h>
    #include <string.h>
    
        
        int main (void){
            
            int num;
            scanf("%d",&num);
            
            int m;
            
            for (m=0;m<num;m++){
                int i;
                int arr[9][9];
                
                int row,col;
                
                for (row=0;row<9;row++){
                    for (col=0;col<9;col++)
                    scanf("%d",&arr[row][col]);
                }
                int jud=1;
                 
                //先判断行列 
                //每行 
                for (row=0;row<9;row++){
                    int a[9];
                    for(i=0;i<9;i++)
                    a[i]=1;
                    for(col=0;col<9;col++){
                        a[arr[row][col]-1]=0;
                    }
                    for (i=0;i<9;i++){
                        if(a[i]==1)
                        jud=0;
                    }
                }
                //每列 
                for (col=0;col<9;col++){
                    int a[9];
                    for(i=0;i<9;i++)
                    a[i]=1;
                    for(row=0;row<9;row++){
                        a[arr[row][col]-1]=0;
                    }
                    for (i=0;i<9;i++){
                        if(a[i]==1)
                        jud=0;
                    }
                }
                
                //后判断九宫格 
                //j,k表示起始点 
                for(int j=0;j<=6;j+=3){
                    for(int k=0;k<=6;k+=3){
                        
                        int a[9];
                        for(i=0;i<9;i++)
                        a[i]=1;
                        
                        for(row=j;row<j+3;row++){
                            for(col=k;col<k+3;col++)
                            a[arr[row][col]-1]=0;
                        }
                        for (i=0;i<9;i++){
                        if(a[i]==1)
                        jud=0;
                    }
                    }
                }
                
                if(jud)
                printf("Right\n");
                else
                printf("Wrong\n");
                getchar();
            }
        }
    
  • 0
    @ 2018-09-07 20:06:18

    #include<iostream>
    using namespace std;
    int main()
    {
    int n,sz[9][9],q[12];
    for(int a=1;a<10;a++)
    q[a]=0;
    cin>>n;
    for(int a;a<n;a++)
    {
    for(int l=0;l<9;l++)//输入数组
    for(int h=0;h<9;h++)
    cin>>sz[l][h];
    for(int l=0;l<9;l++)//判断行
    for(int h=0;h<9;h++)
    {
    q[sz[l][h]]++;
    if(q[sz[l][h]]>1)
    {
    cout<<"Wrong";
    return 0;
    }
    }
    for(int a=1;a<10;a++)
    q[a]=0;
    for(int l=0;l<9;l++) //判断列
    for(int h=0;h<9;h++)
    {
    q[sz[h][l]]++;
    if(q[sz[h][l]]>1)
    {
    cout<<"Wrong";
    return 0;
    }
    }
    for(int a=1;a<10;a++)
    q[a]=0;
    int a=0,b=0;
    while(a<9&&b<9)
    {
    int l,h;
    for(l=a+1;l<a+4;l++)
    for(h=b+1;h<b+4;h++)
    {
    q[sz[h][l]]++;
    if(q[sz[h][l]]>1)
    {
    cout<<"Wrong";
    return 0;
    }
    }
    for(int e=1;e<10;e++)
    q[e]=0;
    a=l;
    b=h;
    if(a==8&&b==8)
    break;
    }
    cout<<"Right";
    return 0;
    }
    }
    哪位大佬帮我看一下出了什么问题

  • 0
    @ 2018-03-15 14:14:30

    #include<iostream>
    using namespace std;
    int a[9999][9999];
    int main()
    {
    int n,x,y,s,d;
    int num1,sum1,sum,num;
    cin>>n;
    for (int i=1;i<=n;i++)
    {

    for (int j=1;j<=9;j++)
    {
    for (int l=1;l<=9;l++)
    {
    cin>>a[j][l];
    }
    }
    int sum=0,num=0;
    for (int j=1;j<=9;j++)
    {
    for (int k=1;k<=8;k++)
    {

    if (a[j][1]==a[j][1+k]) num+=1;
    else num+=0;
    }
    }
    for (int l=1;l<=9;l++)
    {
    for (int k=1;k<=8;k++)
    {

    if (a[l][1]==a[l][1+k]) sum+=1;
    else sum+=0;
    }
    } /*if (num==0&&sum==0) cout<<"Right"<<endl;
    else cout<<"Wrong"<<endl;*/

    /* for (int f=0;f<=6;f=f+3)
    {*/ //cout<<f<<endl;

    for (int j=1;j<=3;j++)
    { //cout<<j<<endl;
    for (int k=1;k<=3;k++)
    { //cout<<k<<endl;
    //cout<<a[j][k]<<endl;

    for (int y=1;y<=3;y++)
    {

    for (int x=1;x<=3;x++)
    {
    // cout<<a[x][y]<<" "<<a[j][k]<<endl;
    if (a[y][x]==a[j][k]&&y!=j&&x!=k)
    {
    sum1+=1;
    }
    else
    {
    sum1+=0;

    }//cout<<sum<<endl;
    }
    }
    }
    }
    for (int j=4;j<=6;j++)
    { //cout<<j<<endl;
    for (int k=1;k<=3;k++)
    { //cout<<k<<endl;
    //cout<<a[j][k]<<endl;

    for (int y=4;y<=6;y++)
    {

    for (int x=1;x<=3;x++)
    {
    // cout<<a[x][y]<<" "<<a[j][k]<<endl;
    if (a[y][x]==a[j][k]&&y!=j&&x!=k)
    {
    sum1+=1;
    }
    else
    {
    sum1+=0;

    }//cout<<sum<<endl;
    }
    }
    }
    }
    for (int j=7;j<=9;j++)
    { //cout<<j<<endl;
    for (int k=1;k<=3;k++)
    { //cout<<k<<endl;
    //cout<<a[j][k]<<endl;

    for (int y=7;y<=9;y++)
    {

    for (int x=1;x<=3;x++)
    {
    // cout<<a[x][y]<<" "<<a[j][k]<<endl;
    if (a[y][x]==a[j][k]&&y!=j&&x!=k)
    {
    sum1+=1;
    }
    else
    {
    sum1+=0;

    }//cout<<sum<<endl;
    }
    }
    }
    }
    for (int j=1;j<=3;j++)
    { //cout<<j<<endl;
    for (int k=4;k<=6;k++)
    { //cout<<k<<endl;
    //cout<<a[j][k]<<endl;

    for (int y=1;y<=3;y++)
    {

    for (int x=4;x<=6;x++)
    {
    // cout<<a[x][y]<<" "<<a[j][k]<<endl;
    if (a[y][x]==a[j][k]&&y!=j&&x!=k)
    {
    sum1+=1;
    }
    else
    {
    sum1+=0;

    }//cout<<sum<<endl;
    }
    }
    }
    }
    for (int j=1;j<=3;j++)
    { //cout<<j<<endl;
    for (int k=7;k<=9;k++)
    { //cout<<k<<endl;
    //cout<<a[j][k]<<endl;

    for (int y=1;y<=3;y++)
    {

    for (int x=7;x<=9;x++)
    {
    // cout<<a[x][y]<<" "<<a[j][k]<<endl;
    if (a[y][x]==a[j][k]&&y!=j&&x!=k)
    {
    sum1+=1;
    }
    else
    {
    sum1+=0;

    }//cout<<sum<<endl;
    }
    }
    }
    }
    for (int j=4;j<=6;j++)
    { //cout<<j<<endl;
    for (int k=4;k<=6;k++)
    { //cout<<k<<endl;
    //cout<<a[j][k]<<endl;

    for (int y=4;y<=6;y++)
    {

    for (int x=4;x<=6;x++)
    {
    //cout<<a[x][y]<<" "<<a[j][k]<<endl;
    if (a[y][x]==a[j][k]&&y!=j&&x!=k)
    {
    sum1+=1;
    }
    else
    {
    sum1+=0;

    }//cout<<sum<<endl;
    }
    }
    }
    }
    for (int j=4;j<=6;j++)
    { //cout<<j<<endl;
    for (int k=7;k<=9;k++)
    { //cout<<k<<endl;
    //cout<<a[j][k]<<endl;

    for (int y=4;y<=6;y++)
    {

    for (int x=7;x<=9;x++)
    {
    //cout<<a[x][y]<<" "<<a[j][k]<<endl;
    if (a[y][x]==a[j][k]&&y!=j&&x!=k)
    {
    sum1+=1;
    }
    else
    {
    sum1+=0;

    }//cout<<sum<<endl;
    }
    }
    }
    }
    for (int j=7;j<=9;j++)
    { //cout<<j<<endl;
    for (int k=4;k<=6;k++)
    { //cout<<k<<endl;
    //cout<<a[j][k]<<endl;

    for (int y=7;y<=9;y++)
    {

    for (int x=4;x<=6;x++)
    {
    //cout<<a[y][x]<<" "<<a[j][k]<<endl;
    if (a[y][x]==a[j][k]&&y!=j&&x!=k)
    {
    sum1+=1;
    }
    else
    {
    sum1+=0;

    }//cout<<sum<<endl;
    }
    }
    }
    }
    for (int j=7;j<=9;j++)
    { //cout<<j<<endl;
    for (int k=7;k<=9;k++)
    { //cout<<k<<endl;
    //cout<<a[j][k]<<endl;

    for (int y=7;y<=9;y++)
    {

    for (int x=7;x<=9;x++)
    {
    // cout<<a[y][x]<<" "<<a[j][k]<<endl;
    if (a[y][x]==a[j][k]&&y!=j&&x!=k)
    {
    sum1+=1;
    }
    else
    {
    sum1+=0;

    }//cout<<sum<<endl;
    }
    }
    }
    }
    /*for (int k=4;k<=6;k++)
    {
    for (int d=4;d<=6;d++)
    {
    for (int x=1;x<=3;x++)
    {
    if (a[x][d]==a[j][k]&&x!=j&&d!=k) sum+=1;
    else sum+=0;
    }
    }
    }
    for (int k=7;k<=9;k++)
    {
    for (int s=7;s<=9;s++)
    {
    for (int x=7;x<=9;x++)
    {
    if (a[x][s]==a[j][k]&&x!=j&&s!=k) sum+=1;
    else sum+=0;
    }
    }
    }*/
    //}
    //}
    if (num==0&&sum==0) cout<<"Right"<<endl;
    else cout<<"Wrong"<<endl;
    }
    return 0;
    }

  • 0
    @ 2018-01-04 14:46:51

    多组数据的问题一定要注意:先读完数据再判断,切忌数据读一半就break!!

    import itertools
    
    
    def _check_unique(vals):
        digit_flag = 1
        for x in vals:
            digit_flag |= (1 << x)
        if digit_flag != (1 << 10) - 1:
            return False
        else:
            return True
    
    
    def check():
        vals = []
    
        row_flag = True
        for i in xrange(9):
            cur_line = map(int, raw_input().split())
            vals.append(cur_line)
            if not _check_unique(cur_line):
                row_flag = False
    
        if not row_flag:
            return False
    
        for c in xrange(9):
            if not _check_unique([vals[r][c] for r in xrange(9)]):
                return False
    
        for c in (0, 3, 6):
            for r in (0, 3, 6):
                cur_vals = [vals[i][j] for (i, j) in itertools.product(range(c, c + 3), range(r, r + 3))] 
                if not _check_unique(cur_vals):
                    return False
    
        return True
    
    
    N = input()
    for i in xrange(N):
        if i > 0:
            raw_input()
        if check():
            print "Right"
        else:
            print "Wrong"
    
    
  • 0
    @ 2017-11-22 23:46:44
    #include <iostream>
    
    using namespace std;
    
    int mat[10][10];  // Sodoku stored mat[1~9][1~9]
    bool book[10]={0};
    
    void readIn()
    {
        for (int i=1; i<=9; i++)
            for (int j=1; j<=9; j++)
                cin >> mat[i][j];
    }
    
    void initBook()
    {
        for (int i=1; i<=9; i++) book[i]=false;
    }
    
    bool check(char where)
    {
        initBook();
        bool ln = (where=='L') ? true : false;
        for (int i=1; i<=9; i++)
        {
            for (int j=1; j<=9; j++)
                if (ln) { if (!book[mat[i][j]]) book[mat[i][j]]=true; else return false; }
                else { if (!book[mat[j][i]]) book[mat[j][i]]=true; else return false; }
            for (int k=1; k<=9; k++)
                if (!book[k]) return false;
            initBook();
        }
        return true;
    }
    
    bool checkSq(int block)
    {
        initBook();
        int sheet[10]={0,1,4,7,1,4,7,1,4,7};
        for (int i=sheet[block]; i<=sheet[block]+2; i++)
            for (int j=sheet[block]; j<=sheet[block]+2; j++)
                if (!book[mat[i][j]]) book[mat[i][j]]=true; else return false;
        return true;
    }
    
    int main()
    {
        int n;
        cin >> n;
        bool isis=true;
        bool result[25]={0};
        int count=0;
        for (int i=1; i<=n; i++)
        {
            readIn();
            isis=(check('L') && check('R'));
            if (isis)
                for (int j=1; j<=9; j++)
                    isis = isis && checkSq(j);
            result[i] = (isis)? true : false;
            count++;
        }
        for (int i=1; i<=count; i++)
            if (result[i]) cout << "Right" << endl;
            else cout << "Wrong" << endl;
    
        return 0;
    }
    
  • 0
    @ 2017-11-15 20:39:40
    //sudoku
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    int read(){
        int x = 0, f = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9'){if (ch == '-') f = -1; ch = getchar();}
        while (ch >= '0' && ch <= '9'){x *= 10; x += ch - '0'; ch = getchar();}
        return x * f;
    }
    
    int sudoku[10][10];
    int n;
    bool vis[10];
    inline bool verified(){
        for (int i = 1; i <= 9; i++){
            memset(vis, 0, sizeof vis);
            for (int j = 1; j <= 9; j++){
                if (vis[sudoku[i][j]]) return false;
                vis[sudoku[i][j]] = true;
            }
        }
        for (int i = 1; i <= 9; i++){
            memset(vis, 0, sizeof vis);
            for (int j = 1; j <= 9; j++){
                if (vis[sudoku[j][i]]) return false;
                vis[sudoku[j][i]] = true;
            }
        }   
        for (int i = 1; i <= 9; i += 3){
            for (int j = 1; j <= 9; j += 3){
                memset(vis, 0, sizeof vis);
                for (int m = i; m < i + 3; m++){
                    for (int n = j; n < j + 3; n++){
                        if (vis[sudoku[m][n]]) return false;
                        vis[sudoku[m][n]] = true;
                    }
                }
            }
        }
        return true;
    }
    
    int main(){
        cout.sync_with_stdio(false);
        n = read();
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= 9; j++){
                for (int k = 1; k <= 9; k++){
                    sudoku[j][k] = read();
                }
            }
            if (verified()){
                cout << "Right" << endl;
            }
            else cout << "Wrong" << endl;
        }
        return 0;
    }
    
    
  • 0
    @ 2017-11-03 21:44:44

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    int n,m,t,i,j,k,s[10][10],c[101],ok;
    int search(int x1,int x2,int y1,int y2)
    {
    int okk=1;
    memset(c,0,sizeof(c));
    int i,j;
    for(i=x1;i<=x2;i++)
    for(j=y1;j<=y2;j++)
    {
    if(!c[s[i][j]])
    c[s[i][j]]=1;
    else
    okk=0;
    }
    if(okk)
    return 1;
    else
    return 0;

    }
    int main()
    {
    cin>>t;
    while(t>0)
    {
    memset(c,0,sizeof(c));
    ok=1;
    for(i=1;i<=9;i++)
    for(j=1;j<=9;j++)
    cin>>s[i][j];
    for(i=1;i<=9;i++)
    {
    memset(c,0,sizeof(c));
    for(j=1;j<=9;j++)
    if(c[s[i][j]]==0)
    c[s[i][j]]=1;
    else
    ok=0;
    }
    for(i=1;i<=9;i++)
    {
    memset(c,0,sizeof(c));
    for(j=1;j<=9;j++)
    if(c[s[j][i]]==0)
    c[s[j][i]]=1;
    else
    ok=0;
    }
    if(ok==0)
    cout<<"Wrong"<<endl;
    else
    {
    int a1x=search(1,3,1,3);
    int a2x=search(1,3,4,6);
    int a3x=search(1,3,7,9);
    int a4x=search(4,6,1,3);
    int a5x=search(4,6,4,6);
    int a6x=search(4,6,7,9);
    int a7x=search(7,9,1,3);
    int a8x=search(7,9,4,6);
    int a9x=search(7,9,7,9);
    if(a1x+a2x+a3x+a4x+a5x+a6x+a7x+a8x+a9x==9)
    cout<<"Right"<<endl;
    else
    cout<<"Wrong"<<endl;
    }
    //cout<<a1x<<a2x<<a3x<<a4x<<a5x<<a6x<<a7x<<a8x<<a9x;
    //else
    //cout<<"Right";
    t--;
    }

    }

  • 0
    @ 2017-10-18 11:14:02

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <algorithm>
    using namespace std;
    int arr[9][9];
    bool check( int a[9] )
    {
    for ( int i = 1 ; i < 9 ; ++ i )
    {
    if ( a[i] != a[i-1]+1 )
    return false;
    }
    return true;
    }
    bool checkRow()
    {
    for ( int i = 0 ; i < 9 ; ++ i )
    {
    int a[9];
    for ( int j = 0 ; j < 9 ; ++ j )
    {
    a[j] = arr[i][j];
    }
    sort( a, a+9 );
    if ( check(a) == false ) return false;
    }
    return true;
    }
    bool checkCol()
    {
    for ( int i = 0 ; i < 9 ; ++ i )
    {
    int a[9];
    for ( int j = 0 ; j < 9 ; ++ j )
    {
    a[j] = arr[i][j];
    }
    sort( a, a+9 );
    if ( check(a) == false ) return false;
    }
    return true;
    }
    bool checkNie()
    {
    for ( int i = 0 ; i < 9 ; i += 3 )
    {
    for ( int j = 0 ; j < 9 ; j += 3 )
    { int a[9];
    for ( int k = 0 ; k < 9 ; ++ k )
    {
    a[k] = arr[i+k/3][j+k%3];
    }

    sort( a, a+9 );
    if ( check(a) == false ) 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", &arr[i][j] );
    }
    }
    if ( checkRow() == false ) {
    cout << "Wrong" << endl;
    continue;
    }
    if ( checkCol() == false ) {
    cout << "Wrong" << endl;
    continue;
    }
    if ( checkNie() == false ) {
    cout << "Wrong" << endl;
    continue;
    }
    cout << "Right" << endl;
    }
    return 0;
    }****

  • 0
    @ 2017-09-26 23:28:20
    #include<cstdio>
    #include<cstring>
    int n;
    int a[9][9];
    bool b[10];
    bool yzjg(int x,int y)
    {
        memset(b,0,sizeof(b));
        for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
            {
                if(b[a[x+i][y+j]])return false;
                b[a[x+i][y+j]]=true;
            }
        return true;
    }
    bool yz()
    {
        for(int i=0;i<9;i++)
        {
            memset(b,0,sizeof(b));
            for(int j=0;j<9;j++)
            {
                if(b[a[i][j]])return false;
                b[a[i][j]]=true;
            }
        }
        for(int j=0;j<9;j++)
        {
            memset(b,0,sizeof(b));
            for(int i=0;i<9;i++)
            {
                if(b[a[i][j]])return false;
                b[a[i][j]]=true;
            }
        }
        for(int i=0;i<9;i+=3)
            for(int j=0;j<9;j+=3)
                if(!yzjg(i,j))return false;
        return true;
    }
    int main()
    {
        scanf("%d",&n);
        while(n--)
        {
            for(int i=0;i<9;i++)
                for(int j=0;j<9;j++)
                    scanf("%d",&a[i][j]);
            if(yz())puts("Right");
            else puts("Wrong");
        }
        return 0;
    }
    
    
  • 0
    @ 2017-09-14 16:06:23
    //vjios 1335 数独
    #include <cstdio>
    #include <cstring>
    using namespace std;
    int arr[9][9],tr[9];
    int main(){
        int number,flag,i,j;
        scanf("%d",&number);
        while(number--){
            flag = 1;
    
            memset(tr,0,sizeof(tr));
    
            for(i=0;i<9;i++)
                for(j=0;j<9;j++){
                    scanf("%d",&arr[i][j]);
                    tr[arr[i][j]-1]++;
                }
            int sum1 = 0,sum=0;
    
            for(i=0;i<9;i++){           //判断每行每列是否和为45;
                int sum1 = 0,sum=0; 
                for(j=0;j<9;j++){
                    sum += arr[i][j];
                    sum1 += arr[j][i];
                }
                if(sum!=45||sum1!=45){
                    flag=0;
                }
            }
    
            sum =0 ;
            for(i=0;i<3;i++)        //预防例2情况出现
                for(j=0;j<3;j++){
                    sum+=arr[i][j];
                }
            if(sum!=45){
                flag=0;
            }
    
            for(int i=0;i<9;i++){   //预防数不全,但和为45的情况出现
                if(tr[i]!=9)
                    flag=0;
            }
    
            if(flag)
                printf("Right\n");
            else
                printf("Wrong\n");
        }
        return 0;
    }
    
  • 0
    @ 2017-09-01 20:58:52

    var
    g:array[1..9,1..9] of integer;//记录数独元素
    b:array[1..9] of boolean;//判重

    procedure init;//读入
    var
    i,j:integer;
    begin
    for i:=1 to 9 do
    begin
    for j:=1 to 9 do
    read(g[i,j]);
    readln;
    end;

    end;

    function line(x:longint):boolean;//判断一行是否合理
    var
    i:integer;
    begin
    fillchar(b,sizeof(b),true);
    for i:=1 to 9 do
    b[g[x,i]]:=false;
    for i:=1 to 9 do
    if(b[i]=true) then exit(false);
    exit(true);
    end;

    function lie(x:longint):boolean;//判断一列是否合理
    var
    i:integer;
    begin
    fillchar(b,sizeof(b),true);
    for i:=1 to 9 do
    b[g[i,x]]:=false;
    for i:=1 to 9 do
    if(b[i]=true) then exit(false);
    exit(true);
    end;

    function sq(x,y:integer):boolean;//判断每个33正方形是否合理
    var
    i,j:integer;
    begin
    fillchar(b,sizeof(b),true);
    for i:=x-2 to x do
    for j:=y-2 to y do
    b[g[i,j]]:=false;
    for i:=1 to 9 do
    if(b[i]=true) then exit(false);
    exit(true);
    end;

    procedure work;//运行
    var
    i,j:integer;
    x:boolean;//x作为可能性判断的值
    begin
    x:=true;
    for i:=1 to 9 do begin x:=x and line(i) and lie(i); end;
    for i:=1 to 3 do
    for j:=1 to 3 do
    x:=x and sq(i*3,j*3);//采用and函数比较
    if(x) then writeln('Right')
    else
    writeln('Wrong');
    end;

    var
    n,i:integer;
    begin
    readln(n);
    for i:=1 to n do
    begin
    init;
    work;
    end;

    end.

  • 0
    @ 2017-07-06 11:40:42

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int read()
    {
    int x=0;char c;int f=1;
    for(c=getchar();c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
    for(;c>='0'&&c<='9';c=getchar())x=(x<<3)+(x<<1)+c-'0';
    return x*f;
    }
    int n,a[10][10],heng[10],zong[10],num[10];
    inline void check()
    {
    int fla;
    int s;bool sign=0;
    for(int i=1;i<=9;i++)
    {
    memset(heng,0,sizeof(heng));
    for(int j=1;j<=9;j++)
    {
    heng[a[i][j]]++;
    if(heng[a[i][j]]>1){
    sign=1;
    break;
    }
    }
    if(sign)break;
    }
    if(!sign)
    {
    for(int i=1;i<=9;i++)
    {
    memset(zong,0,sizeof(zong));
    for(int j=1;j<=9;j++)
    {
    zong[a[j][i]]++;
    if(zong[a[j][i]]>1)
    {
    sign=1;
    break;
    }
    }
    if(sign)break;
    }
    }
    if(!sign)
    for(int i=0;i<=8;i++)
    {
    memset(num,0,sizeof(num));
    for(int j=1;j<=3;j++)
    {
    for(int k=1;k<=3;k++)
    {
    num[a[j+3*(i%3)][k+3*(i/3)]]++;
    if(num[a[j+3*(i%3)][k+3*(i/3)]]>1)
    {
    sign=1;
    break;
    }
    }
    if(sign)break;
    }
    if(sign)break;
    }
    if(sign)cout<<"Wrong"<<endl;
    else cout<<"Right"<<endl;
    }
    int main()
    {
    n=read();
    for(int i=1;i<=n;i++)
    {
    memset(a,0,sizeof(a));
    for(int j=1;j<=9;j++)
    {
    for(int k=1;k<=9;k++)
    {
    a[j][k]=read();
    }
    }
    check();
    }
    }

  • 0
    @ 2017-06-24 10:06:15

    #include <cstring>
    #include <cstdio>
    #include <iostream>

    using namespace std;

    bool ninvis(int arr[][9],int begr,int begc,int endr,int endc){
    if(endc-begc==3&&endr-begr==3){
    bool vis[10]={0};
    for(int i=begr;i<endr;++i){
    for(int j=begc;j<endc;++j){
    vis[arr[i][j]]=1;
    }
    }
    for(int i=1;i<=9;++i){
    if(!vis[i]){
    return false;
    }
    }
    return true;
    }
    for(int i=0;i<=6;i+=3){
    for(int j=0;j<=6;j+=3){
    if(!ninvis(arr,i,j,i+3,j+3)){
    return false;
    }
    }
    }
    return true;
    }

    int main(){
    int nums[9][9]={0},n=0;
    bool isvalid=true;
    cin>>n;
    while(n--){
    bool rowvis[9][10]={0};
    bool colvis[9][10]={0};
    for(int i=0;i<9;++i){
    for(int j=0;j<9;++j){
    cin>>nums[i][j];
    rowvis[i][nums[i][j]]=1;
    colvis[j][nums[i][j]]=1;
    }
    }
    for(int i=0;i<9;++i){
    for(int j=1;j<=9;++j){
    if(!rowvis[i][j]||!colvis[i][j]){
    isvalid=false;
    break;
    }
    }
    }
    isvalid=isvalid&&ninvis(nums,0,0,9,9);
    if(isvalid)cout<<"Right\n";
    else cout<<"Wrong\n";
    getchar();
    isvalid=true;
    }
    //system("pause");
    return 0;
    }

  • 0
    @ 2017-06-04 11:07:34
    #include<cstdio>
    #include<string>
    #include<set>
    using namespace std;
    int n = 0;
    int map[9][9] = { 0 };
    int  judge()
    {
        set<int> s;
        int i = 0, j = 0;
        for (i = 0; i < 9; i++)  //行
        {
            s.clear();
            for (j = 0; j < 9; j++)
            {
                s.insert(map[i][j]);
                
            }
            
            if (s.size() != 9)
                return 0;
        }
        
        for (i = 0; i < 9; i++)
        {
            s.clear();
            for (j = 0; j < 9; j++)
            {
                s.insert(map[j][i]);
                
            }
            
            if (s.size() != 9)
                return 0;
    
        }
        
        for (i = 0; i < 9; i+=3)
        {
            for (j = 0; j < 9; j+=3)
            {
                s.clear();
                for (int k = 0; k < 3;k++)
                for (int m = 0; m < 3; m++)
                {
                    s.insert(map[i + k][j + m]);
                    
                }
                
                if (s.size() != 9)
                    return 0;
            }
        }
        return 1;
    }
    int main()
    {
    
        //freopen("data.in", "r", stdin);
        //freopen("data.out", "w", stdout);
        int i, j = 0;
        scanf("%d", &n);
        while (n--)
        {
            for (i = 0; i < 9; i++)
            {
                for (j = 0; j < 9; j++)
                {
                    scanf("%d", &map[i][j]);
                    
                }
                
            }
            if (judge()) printf("Right\n");
            else printf("Wrong\n");
        }
        
        
        return 0;
    }
    

信息

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