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

poj2676(DFS)

时间:2018-03-11 20:54:47      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:close   i++   ssi   uniq   暴力   图片   imp   c99   color   

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("\n");
        }
    }
    return 0;
}
View Code

 



poj2676(DFS)

标签:close   i++   ssi   uniq   暴力   图片   imp   c99   color   

原文地址:https://www.cnblogs.com/ACRykl/p/8545368.html

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