zoukankan      html  css  js  c++  java
  • POJ Sudoku 数独填数 DFS

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 18105   Accepted: 8772   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

    暑假艾教教的数独,留个板子。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    int sudoku[10][10];
    int column[10][10];
    int row[10][10];
    int nine[10][10];
    struct node
    {
        int x,y;
    };
    node ready[105];
    int T;
    int num = 0;
    char s[12][12];
    int cal(int x,int y)
    {
        return (x-1)/3*3+(y-1)/3+1;
    }
    bool cmp(node A,node B)
    {
        return A.y>B.y;
    }
    void put(int x,int y,int tt,int flag)
    {
        if(flag) sudoku[x][y] = tt;
        row[x][tt] = flag;
        column[y][tt] = flag;
        nine[cal(x,y)][tt] = flag;
    }
    int ok = 0;
    int can(int x,int y,int tt)
    {
        if(row[x][tt]) return 0;
        if(column[y][tt]) return 0;
        if(nine[cal(x,y)][tt]) return 0;
        return 1;
    }
    void dfs(int k)
    {
        if(ok) return;
        if(k==num+1)
        {
            for(int i=1;i<=9;i++)
            {
                for(int j=1;j<=9;j++)
                {
                    if(j==9) printf("%d
    ",sudoku[i][j]);
                    else printf("%d",sudoku[i][j]);
                }
            }
            ok = 1;
            return;
        }
        for(int nex=1;nex<=9;nex++)
        {
            if(can(ready[k].x,ready[k].y,nex))
            {
                put(ready[k].x,ready[k].y,nex,1);
                dfs(k+1);
                put(ready[k].x,ready[k].y,nex,0);
            }
        }
    }
    int main()
    {
        cin>>T;
        while(T--)
        {
            ok = 0;
            memset(sudoku,0,sizeof(sudoku));
            memset(ready,0,sizeof(ready));
            memset(column,0,sizeof(column));
            memset(row,0,sizeof(row));
            memset(nine,0,sizeof(nine));
            num = 0;
            for(int i=1;i<=9;i++)
            {
                scanf("%s",s[i]+1);
                for(int j=1;j<=9;j++)
                {
                    if(s[i][j]=='0')
                    {
                        num++;
                        ready[num].x = i;
                        ready[num].y = j;
                    }
                    else
                    {
                        put(i,j,s[i][j]-'0',1);
                    }
                }
            }
            sort(ready+1,ready+num+1,cmp);
            dfs(1);
        }
        return 0;
    }
    
    
    
     
  • 相关阅读:
    [java核心篇02]__内部类
    iOS学习之MVC,MVVM,MVP模式优缺点
    iOS MVC、MVP和MVVM理解
    网络常见的9大命令
    计算机网络基础知识总结
    iOS面试常见问题和知识点汇总
    NSMutableArray 删除可变数组元素
    iOS之集成GoogleMap定位、搜索注意事项
    iOS 13-Sign In with Apple
    Lipo移除ORC架构
  • 原文地址:https://www.cnblogs.com/littlepear/p/5789058.html
Copyright © 2011-2022 走看看