码迷,mamicode.com
首页 > 其他好文 > 详细

POJ Sudoku 数独填数 DFS

时间:2016-08-19 23:55:03      阅读:496      评论:0      收藏:0      [点我收藏+]

标签:

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\n",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;
}

 

 

POJ Sudoku 数独填数 DFS

标签:

原文地址:http://www.cnblogs.com/littlepear/p/5789058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!