poj 2676 Sudoku dfs

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

题意:

就是简单的数独游戏加上3*3方格。

在一个三乘三的方格里和一列和一行填1——9的数字, 但是不能出现相同的数字。

思路:

这里建三个二维数组

hang[10][10]

lie[10][10]

dui[10][10]

储存1——9是否用过了

就是将整个方格里面的挨个点遍历一遍。

先挨个点判断是否填上数字了, 填上了则继续遍历, 没填上, 则挨个1——9遍历,当三个数组都没有当前数字时, 就在方格里填上数字, 然后继续遍历,当没有符合条件的是, 那就返回上一级( 回溯), 然后让上一级再挑选一个合适的数字。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int n;
char s[10][15];
int mapp[10][10];
int hang[10][10];
int shu[10][10];
int dui[10][10];
int sudu(int locx,int locy)
{
    if(locx==9)
        return 1;
    int flag=0;
    if(mapp[locx][locy])
    {
        if(locy==8)
            flag=sudu(locx+1,0);
        else
            flag=sudu(locx,locy+1);
        if(flag)
            return 1;
        else
            return 0;
    }
    else
    {
        int k=3*(locx/3)+(locy/3)+1;
        for (int i=1;i<=9;i++)
        {
            if(!hang[locx][i]&&!shu[locy][i]&&!dui[k][i])
            {
                mapp[locx][locy]=i;
                hang[locx][i]=1;
                shu[locy][i]=1;
                dui[k][i]=1;
                if(locy==8)
                    flag=sudu(locx+1,0);
                else
                    flag=sudu(locx,locy+1);
                if(flag)
                    return 1;
                else
                {
                    mapp[locx][locy]=0;
                    hang[locx][i]=0;
                    shu[locy][i]=0;
                    dui[k][i]=0;
                }
            }
        }
        return 0;
    }
}
int main()
{
    scanf("%d",&n);
    while (n--)
    {
        memset (hang,0,sizeof(hang));
        memset (shu,0,sizeof(shu));
        memset (dui,0,sizeof(dui));
        for (int i=0;i<9;i++)
            {
                scanf("%s",s[i]);
            }
        for (int i=0;i<9;i++)
        {
            for (int j=0;j<9;j++)
             {
              int k=3*(i/3)+(j/3)+1;
              mapp[i][j]=s[i][j]-'0';
              //printf("%d",mapp[i][j]);
              hang[i][mapp[i][j]]=1;
              shu[j][mapp[i][j]]=1;
              dui[k][mapp[i][j]]=1;
             }
             //printf("\n");
        }
        sudu (0,0);
        for (int i=0;i<9;i++)
        {
            for (int j=0;j<9;j++)
                printf("%d",mapp[i][j]);
            printf("\n");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/81385757