zoukankan      html  css  js  c++  java
  • poj2676(DFS)

    Sudoku
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 21946   Accepted: 10406   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

    分析:DFS暴力啊。。。

    #include<cstdio>
    #include<cstring>
    int map[10][10];
    int node[100][2];//记录空格
    int c[10][10],r[10][10];//记录,行列状态
    int g[10][10];//3*3,9个3*3的格见下图 
    /*
    111222333
    111222333
    111222333
    444555666
    444555666
    444555666
    777888999
    777888999
    777888999
    */
    int N,flag;
    void dfs(int cur)
    {
        if(cur==N)
        {
            flag=1;
            return;
        }
        int x=node[cur][0],y=node[cur][1];
        int t=(y-1)/3+(x-1)/3*3+1;
        for(int i=1;i<10;i++)
        {
            if(!c[x][i]&&!r[y][i]&&!g[t][i])
            {
                map[x][y]=i;
                c[x][i]=r[y][i]=g[t][i]=1;
                dfs(cur+1);
                if(flag) return;
                c[x][i]=r[y][i]=g[t][i]=0;
                map[x][y]=0;
            }
        }
        return;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            flag=N=0;
            memset(c,0,sizeof(c));
            memset(r,0,sizeof(r));
            memset(g,0,sizeof(g));
            for(int i=1;i<10;i++)
            {
                for(int j=1;j<10;j++)
                {
                    scanf("%1d",&map[i][j]);
                    //记录空格
                    if(map[i][j]==0)
                    {
                        node[N][0]=i;
                        node[N][1]=j;
                        N++;
                    }
                    else
                    {
                        c[i][map[i][j]]++;//第i行有了map[i][j]这个数
                        r[j][map[i][j]]++;//第j列有了map[i][j]这个数
                        g[(j-1)/3+(i-1)/3*3+1][map[i][j]]++;//3*3 
                    }
                }
            }
            
            dfs(0);
            for(int i=1;i<10;i++)
            {
                for(int j=1;j<10;j++)
                printf("%d",map[i][j]);
                printf("
    ");
            }
        }
        return 0;
    }
    View Code


  • 相关阅读:
    Airtest操作多台云手机,进行分发,多个脚本后端调度
    Airtest常见的元素定位不到
    pythonDES加密与解密以及hex输出和bs64格式输出
    adb命令将抓包工具证书从用户目录移动至系统目录,解决反爬对于本地证书认证
    解决appium每次app启动时候已登入账号会登出
    PHP数组循环遍历的几种方式
    TP5.1/TP框架的访问控制,访问不存在的模块、控制器、方法等控制
    PHP常用数组函数
    TP5截取部分字符串
    apache虚拟主机配置及解析
  • 原文地址:https://www.cnblogs.com/ACRykl/p/8545368.html
Copyright © 2011-2022 走看看