POJ 2676 Sudoku(DFS)

Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16024   Accepted: 7824   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.
POJ 2676 Sudoku(DFS)_第1张图片

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

思路:正向或者逆向枚举81个小的cell,当遇到cell的值为0时,试填1-9个数,看是否满足要求(即3*3的cell,行与列都不同)难点就是控制程序的运行了

#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<cmath>
#include<queue>
#define LL long long

using namespace std;

int n,l;
int Map[10][10];
char s[10][10];
bool flag;
int ans;

bool judge(int x,int y)
{
    int i,j,k;
    for(i=x/3*3; i<x/3*3+3; i++)
    {
        for(j=y/3*3; j<y/3*3+3; j++)
        {
            if(i==x&&y==j)
                continue;
            else if(Map[x][y]==Map[i][j])
                return false;
        }
    }
    k=Map[x][y];
    for(j=0; j<9; j++)
    {
        if(j==y)
            continue;
        if(Map[x][j]==k)
            return false;
    }
    for(i=0; i<9; i++)
    {
        if(i==x)
            continue;
        if(Map[i][y]==k)
            return false;
    }
    return true;
}
int dfs(int n)
{
    int i,j,k;
    if(n<0)//利用标记变量来控制回溯
    {
        flag=true;
        return 1;
    }
    if(Map[n/9][n%9]!=0)//如果当前的cell不为0继续递归下一个cell
         dfs(n-1);
    else//否则枚举当前的情况
    {
        for(i=1;i<=9;i++)
        {
            Map[n/9][n%9]=i;
            if(judge(n/9,n%9))//如果符合上述的3个条件
            {
                dfs(n-1);//继续递归下一个cell,与此同时 有不符合条件的,直到在全部搜索完之后,如果枚举完1-9所有的数后仍不符合条件就会返回0从而回溯改变以前的值
                if(flag)//如果已经找到标记下来,一直返回到底
                    return 1;
            }
            Map[n/9][n%9]=0;
        }
    }
    return 0;
}
int main()
{
    int cla,i,j;
    scanf("%d",&cla);
    getchar();
    while(cla--)
    {
        flag=false;
        for(i=0; i<9; i++)
        {
            gets(s[i]);
            for(j=0; j<9; j++)
            {
                Map[i][j]=s[i][j]-'0';
            }
        }
        dfs(80);
        for(i=0; i<9; i++)
        {
            for(j=0; j<9; j++)
            {
                printf("%d",Map[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(C语言,ACM,poj,DFS)