poj2676——Sudoku(深度搜索)

本文介绍了一种用于解决数独谜题的暴力搜索算法。通过记录未填充的单元格坐标并使用深度优先搜索策略,尝试填充数字以满足数独规则。此方法简单直接,适用于小型数据集。

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

数独应该都会玩,还好数据规模不大,可以直接暴力过。思路是,记录下每个为0的点,用v数组记录下它的坐标,在dfs函数里猜数,用judge函数判断是否与其他点重复

#include <iostream>
#include <cstdio>
using namespace std;
int v[90][2],map[10][10];
bool judge(int x,int y,int k)
{
    int i,j,sx,sy;
    for(i=0; i<9; ++i)  //在这个数所在的行列找是否有相同的数
    {
        if(map[i][y]==k)
            return false;
        if(map[x][i]==k)
            return false;
    }
    //在这个数所在的小九宫格找是否有相同的的数
    sx=(x/3)*3;
    sy=(y/3)*3;
    for(i=0; i<3; ++i)
        for(j=0; j<3; ++j)
            if(map[sx+i][sy+j]==k)
                return false;
    return true;  //都没找到,说明可以放这个数
}
bool dfs(int cnt)
{
    int i,x,y;
    if(cnt<0)  //搜索完毕
        return true;
    for(i=1; i<10; ++i)  //i从1到9猜数
    {
        x=v[cnt][0];
        y=v[cnt][1];
        if(judge(x,y,i))  //x,y是为0的数字的坐标,i是假设放的数,判断是否可以放
        {
            map[x][y]=i;   //可以就改变这个数
            if(dfs(cnt-1))  //进行下一个数的搜索
                return true;
            map[x][y]=0;  //如果进行到这,说明放当前i的话,在后面会有冲突,所以重置
        }
    }
    return false;
}
int main()
{
    int t,i,j,num;
    char c;
    scanf("%d\n",&t);
    while(t--)
    {
        num=0;
        for(i=0; i<9; ++i,getchar())
            for(j=0; j<9; ++j)
            {
                scanf("%c",&c);
                map[i][j]=c-'0';
                if(map[i][j]==0)
                {
                    v[num][0]=i;  //v数组记录下这个为0的数的坐标
                    v[num++][1]=j;
                }
            }
        dfs(num-1);  //从最后一个为0的数开始搜索
        for(i=0; i<9; ++i)
        {
            for(j=0; j<9; ++j)
                printf("%d",map[i][j]);
            printf("\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值