• Sudoku


    Sudoku
    Time Limit: 2000MS Memory Limit: 65536K
    Total Submissions: 15952 Accepted: 7791 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
    Southeastern Europe 2005
    这个题要好好反思一下,因为我将题的思路想了一半,感觉有点不太可行,就放弃了这个方案,找的题解,以后要对自己有点信心,勇敢去敲

    #include <map>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <string>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int INF =0x3f3f3f3f;
    const int Max =120;
    char str[11][11];
    bool a[11][11];
    bool b[11][11];
    bool c[11][11];
    int Map[11][11];
    bool DFS(int x,int y)
    {
        if(x==9)
        {
            return true;
        }
        bool flag=false;
        if(Map[x][y])
        {
            if(y==8)
            {
                flag=DFS(x+1,0);
            }
            else
            {
                flag=DFS(x,y+1);
            }
            if(flag)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            int k=3*(x/3)+y/3;
    
            for(int i=1; i<=9; i++)
            {
                if(!a[x][i]&&!b[y][i]&&!c[k][i])
                {
                    Map[x][y]=i;
                    a[x][i]=true;
                    b[y][i]=true;
                    c[k][i]=true;
                    if(y==8)
                    {
                        flag=DFS(x+1,0);
                    }
                    else
                    {
                        flag=DFS(x,y+1);
                    }
                    if(!flag)
                    {
                        Map[x][y]=0;
                        a[x][i]=false;
                        b[y][i]=false;
                        c[k][i]=false;
                    }
                    else
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            for(int i=0; i<9; i++)
            {
                scanf("%s",str[i]);
            }
            memset(a,false,sizeof(a));
            memset(b,false,sizeof(b));
            memset(c,false,sizeof(c));
            memset(Map,0,sizeof(Map));
            for(int i=0; i<9; i++)
            {
                for(int j=0; j<9; j++)
                {
                    Map[i][j]=str[i][j]-'0';
                    if(Map[i][j])
                    {
                        a[i][Map[i][j]]=true;
                        b[j][Map[i][j]]=true;
                        int k=3*(i/3)+j/3;
                        c[k][Map[i][j]]=true;
                    }
                }
            }
            DFS(0,0);
            for(int i=0; i<9; i++)
            {
                for(int j=0; j<9; j++)
                    cout<<Map[i][j];
                cout<<endl;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Java入门3.2---线程池
    Java入门3.1---多线程
    打开ppt报"powerpoint无法加载mathtype加载项"错误
    LATEX排版总结
    casbin 权限系统
    Go netpoll I/O 多路复用构建原生网络模型之源码深度解析
    使用winsw包装服务将nginx包装为Windows服务
    Node.js 的模块系统
    一文读懂 babel7 的配置文件加载逻辑
    vue-cli是什么?和 webpack是什么关系?
  • 原文地址:https://www.cnblogs.com/juechen/p/5256009.html
Copyright © 2020-2023  润新知