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


  • 相关阅读:
    提交订单的时候,实现拦截,登录成功后跳转回之前的页面;使用redis生成指定起始位置的id
    iOS Outlets Referencing Outlets
    iOS 常用控件 参数
    iOS UIImageView设置为圆形
    iOS Xcode个人常用插件
    iOS Xcode注释的几种使用方法
    MFC MSBDutyTable下载地址
    MFC HTTP访问URL
    MFC 程序以管理员权限运行
    MFC 打开文件夹选择框并获取文件夹路径
  • 原文地址:https://www.cnblogs.com/ACRykl/p/8545368.html
Copyright © 2011-2022 走看看