zoukankan      html  css  js  c++  java
  • POJ

    Sudoku

    Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task. 

    Input

    The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

    Output

    For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

    Sample Input

    1
    103000509
    002109400
    000704000
    300502006
    060000050
    700803004
    000401000
    009205800
    804000107

    Sample Output

    143628579
    572139468
    986754231
    391542786
    468917352
    725863914
    237481695
    619275843
    854396127


    数独游戏。感觉这道题的代码还是蛮实用的~思路是dfs从头到尾依次枚举每个0点,尝试填入1->9,不合适再返回上层,直到填满0为止。
    还有就是反搜(0ms),虽然我不会证明,但是和正搜(891ms)对比,是不是很神奇?

    #include<stdio.h>
    #include<string.h>
    
    char aa[10][10];
    int a[10][10];
    int row[10][10],col[10][10],squ[10][10];
    int c1,c2,f;
    
    int jud(int x,int y)
    {
        if(x<=3&&y<=3) return 1;
        if(x<=3&&4<=y&&y<=6) return 2;
        if(x<=3&&7<=y) return 3;
        if(4<=x&&x<=6&&y<=3) return 4;
        if(4<=x&&x<=6&&4<=y&&y<=6) return 5;
        if(4<=x&&x<=6&&7<=y) return 6;
        if(7<=x&&y<=3) return 7;
        if(7<=x&&4<=y&&y<=6) return 8;
        if(7<=x&&7<=y) return 9;
    }
    
    void dfs()
    {
        int i,j,k;
        if(f==1) return;
        if(c1==c2){
            f=1;
            for(i=1;i<=9;i++){
                for(j=1;j<=9;j++){     //反搜枚举9->1
                    printf("%d",a[i][j]);
                }
                printf("
    ");
            }
            return;
        }
        for(i=1;i<=9;i++){
            for(j=1;j<=9;j++){
                if(a[i][j]==0){
                    for(k=1;k<=9;k++){
                        if(row[i][k]==0&&col[j][k]==0&&squ[jud(i,j)][k]==0){
                            row[i][k]=1;
                            col[j][k]=1;
                            squ[jud(i,j)][k]=1;
                            a[i][j]=k;
                            c2++;
                            dfs();
                            row[i][k]=0;
                            col[j][k]=0;
                            squ[jud(i,j)][k]=0;
                            a[i][j]=0;
                            c2--;
                        }
                    }
                    return;
                }
            }
        }
    }
    
    int main()
    {
        int t,i,j;
        scanf("%d",&t);
        while(t--){
            memset(a,0,sizeof(a));
            memset(row,0,sizeof(row));
            memset(col,0,sizeof(col));
            memset(squ,0,sizeof(squ));
            c1=0;
            for(i=0;i<9;i++){
                getchar();
                scanf("%s",aa[i]);
                for(j=0;j<9;j++){
                    a[i+1][j+1]=aa[i][j]-'0';
                    if(a[i+1][j+1]==0) c1++;
                    else{
                        row[i+1][a[i+1][j+1]]=1;
                        col[j+1][a[i+1][j+1]]=1;
                        squ[jud(i+1,j+1)][a[i+1][j+1]]=1;
                    }
                }
            }
            c2=0;f=0;
            dfs();
        }
        return 0;
    }
  • 相关阅读:
    Python的历史
    python excel
    excel xdr wdr
    sql 常用命令
    selenium 配置firefox
    SQL 一直恢复状态解决方法
    sqlserver 学习
    ITCHAT用法
    健身卡属性,以及业务规则,
    安装REDIS
  • 原文地址:https://www.cnblogs.com/yzm10/p/7245375.html
Copyright © 2011-2022 走看看