zoukankan      html  css  js  c++  java
  • poj2676 Sudoku

    Sudoku
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 17953   Accepted: 8688   Special Judge

    Description

    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

    Source

    最近重装电脑,换成linux的ubuntu系统,还学了emacs的一点皮毛,被弄的晕头转向。。。

    题目要ac倒是没有什么难度,最裸的暴力,没有加什么剪枝。分分钟水过。

    15854066 ksq2013 2676 Accepted 704K 844MS G++ 1849B 2016-07-30 21:30:13
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    using namespace std;
    int thd[4]={0,3,6,9},sdk[10][10];
    bool col[10][10],row[10][10];
    bool jud(int x,int y,int z)
    {
        int xs,xt,ys,yt;
        for(int i=1;i<=3;i++)
            if(x<=thd[i]){
                xs=thd[i-1]+1;
                xt=thd[i];
                break;
            }
        for(int i=1;i<=3;i++)
            if(y<=thd[i]){
                ys=thd[i-1]+1;
                yt=thd[i];
                break;
            }
        for(int i=xs;i<=xt;i++)
            for(int j=ys;j<=yt;j++)
                if(!(sdk[i][j]^z))
                    return false;//this answer is ;
        return true;
    }
    bool dfs()
    {
        for(int i=1;i<=9;i++)
            for(int j=1;j<=9;j++)
                if(!sdk[i][j]){
                    for(int k=1;k<=9;k++){
                        if((!col[j][k])&&(!row[i][k])&&jud(i,j,k)){
                            sdk[i][j]=k;
                            col[j][k]=row[i][k]=1;
                            if(dfs())return true;//selected the right answer;
                            col[j][k]=row[i][k]=0;
                            sdk[i][j]=0;
                        }
                    }
                    return false;//it was impossible to deduce an answer in a certain unit,then it's unreachable;
                }
        return true;//the units were filled correctly;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--){
            memset(col,false,sizeof(col));
            memset(row,false,sizeof(row));
            for(int i=1;i<=9;i++)
                for(int j=1;j<=9;j++){
                    char ch;
                    cin>>ch;
                    sdk[i][j]=ch-'0';
                    col[j][sdk[i][j]]=true;
                    row[i][sdk[i][j]]=true;//Init();
                }
            dfs();
            for(int i=1;i<=9;i++){
                for(int j=1;j<=9;j++)
                    printf("%d",sdk[i][j]);
                putchar('
    ');
            }
        }
        return 0;
    }

    看了网上的题解后,发现反着搜,即从右下角向左上角搜更快逼近正解,于是稍稍修改了程序,果然快了许多倍。

    15854143 ksq2013 2676 Accepted 704K 16MS G++ 1849B 2016-07-30 21:44:56
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    using namespace std;
    int thd[4]={0,3,6,9},sdk[10][10];
    bool col[10][10],row[10][10];
    bool jud(int x,int y,int z)
    {
        int xs,xt,ys,yt;
        for(int i=1;i<=3;i++)
            if(x<=thd[i]){
                xs=thd[i-1]+1;
                xt=thd[i];
                break;
            }
        for(int i=1;i<=3;i++)
            if(y<=thd[i]){
                ys=thd[i-1]+1;
                yt=thd[i];
                break;
            }
        for(int i=xs;i<=xt;i++)
            for(int j=ys;j<=yt;j++)
                if(!(sdk[i][j]^z))
                    return false;//this answer is ;
        return true;
    }
    bool dfs()
    {
        for(int i=9;i>=1;i--)
            for(int j=9;j>=1;j--)
                if(!sdk[i][j]){
                    for(int k=1;k<=9;k++){
                        if((!col[j][k])&&(!row[i][k])&&jud(i,j,k)){
                            sdk[i][j]=k;
                            col[j][k]=row[i][k]=1;
                            if(dfs())return true;//selected the right answer;
                            col[j][k]=row[i][k]=0;
                            sdk[i][j]=0;
                        }
                    }
                    return false;//it was impossible to deduce an answer in a certain unit,then it's unreachable;
                }
        return true;//the units were filled correctly;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--){
            memset(col,false,sizeof(col));
            memset(row,false,sizeof(row));
            for(int i=1;i<=9;i++)
                for(int j=1;j<=9;j++){
                    char ch;
                    cin>>ch;
                    sdk[i][j]=ch-'0';
                    col[j][sdk[i][j]]=true;
                    row[i][sdk[i][j]]=true;//Init();
                }
            dfs();
            for(int i=1;i<=9;i++){
                for(int j=1;j<=9;j++)
                    printf("%d",sdk[i][j]);
                putchar('
    ');
            }
        }
        return 0;
    }



  • 相关阅读:
    ssh工具推荐MobaXterm 可能是你遇到过的比较出色的一款
    IDEA不能读取配置文件,springboot配置文件无效、IDEA resources文件夹指定
    修改设置notepad++默认保存文件格式
    java jdk 8反编译工具JD-GUI、procyon-decompiler、luyten、crf下载使用简介
    Synchronized锁性能优化偏向锁轻量级锁升级 多线程中篇(五)
    java并发多线程显式锁Condition条件简介分析与监视器 多线程下篇(四)
    java多线程Lock接口简介使用与synchronized对比 多线程下篇(三)
    java 并发多线程 锁的分类概念介绍 多线程下篇(二)
    java 并发多线程显式锁概念简介 什么是显式锁 多线程下篇(一)
    sql修改二进制加密密码
  • 原文地址:https://www.cnblogs.com/keshuqi/p/5957729.html
Copyright © 2011-2022 走看看