• POJ 2676 Sudoku(暴搜)




    Sudoku
    Time Limit: 2000MSMemory Limit: 65536K
    Total Submissions: 11672Accepted: 5801Special 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. 
    POJ 2676 Sudoku(暴搜) - qhn999 - 码代码的猿猿

    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




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

    using namespace std;

    int visr[11][11];
    int visc[11][11];
    int visv[4][4][11];
    int mp[10][10];
    char mmp[10][10];

    int OK=0;

    void dfs(int x,int y)
    {
        if(OK) return ;
        if(x>9&&!OK)//不要忘了最后一格
        {
            for(int i=1;i<=9;i++)
            {
                for(int j=1;j<=9;j++)
                {
                    printf("%d",mp[j]);
                }
                putchar(10);
            }
            OK=1;
            return ;
        }
        int tx,ty;

        if(mp[x][y]!=0)
        {
            ty=y+1;
            tx=x;
            if(ty==10)
            {
                tx++;
                ty=1;
            }
            dfs(tx,ty);
        }
        else if(mp[x][y]==0)
        {
            for(int i=1;i<=9;i++)
            {
                if(visr[x]==0&&visc[y]==0&&visv[(x-1)/3+1][(y-1)/3+1]==0)
                {
                    visr[x]=1;
                    visc[y]=1;
                    visv[(x-1)/3+1][(y-1)/3+1]=1;
                    tx=x;ty=y+1;
                    if(ty==10)
                    {
                        ty=1;
                        tx++;
                    }
                    mp[x][y]=i;
                    dfs(tx,ty);
                    mp[x][y]=0;
                    visr[x]=0;
                    visc[y]=0;
                    visv[(x-1)/3+1][(y-1)/3+1]=0;
                }
            }
        }

    }

    int main()
    {
        int T;
        scanf("%d",&T);
    while(T--)
    {
        memset(visr,0,sizeof(visr));
        memset(visc,0,sizeof(visc));
        memset(visv,0,sizeof(visv));
        memset(mp,0,sizeof(mp));
        memset(mmp,'0',sizeof(mmp));

        for(int i=1;i<=9;i++)
        {
            scanf("%s",mmp);
        }

        for(int i=1;i<=9;i++)
        {
            for(int j=1;j<=9;j++)
            {
                char c=mmp[j-1];
                mp[j]=c-'0';
                if(mp[j]!=0)
                {
                    visr[mp[j]]=1;
                    visc[j][mp[j]]=1;
                    visv[(i-1)/3+1][(j-1)/3+1][mp[j]]=1;
               }
            }
        }

        OK=0;
        dfs(1,1);
    }

        return 0;
    }

  • 相关阅读:
    富文本的一般处理方式,document.getElementById('富文本的ID').contentWindow.document.body.innerHTML = '%s'" %(content)
    本地搭建Jenkins
    if __name__ == '__main__'是什么意思?如何理解?看到一个很有用的解答
    关于执行webdriver.Chrome; 报错WebDriverException: Message: unknown error: Element is not clickable at point (1085, 103)
    【转载】学习总结——接口测试基础
    fiddler使用——配置抓取https,出现提示“禁用解密”“单击配置”
    算法(二叉树-矩阵-堆排序)
    算法(递归)---写的很乱
    ES6高级技巧(二)
    ES6高级使用技巧(reduce,filter篇)
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350983.html
Copyright © 2020-2023  润新知