poj2676 Sudoku

本文介绍了一种解决数独问题的简单算法。通过递归搜索和状态标记的方式,确保每行、每列及每个3x3的小方格内的数字都不重复。文章提供了完整的C++实现代码,并解释了关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 17913 Accepted: 8674 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

题意就是输入t组数独,空格用0表示,让你找出一个可行的解。

我们要填的就是空缺的位置,所以穷举每个空缺的位置,从1-9,只要能放下一个数字,就去搜下一个空位置,直到空位置填满,如果能填满说明数独就已经完全解开了。

搜索的时候就是无脑的暴力空位置就可以,9*9很小。然后对于每个位置,状态限制有3:

1.每行每个数字只能出现一次

2.每列每个数字只能出现一次

3.每个9宫格每个数字只能出现一次

用数组标记就可以。

具体看代码吧

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
const int MAXN=10+5;
char tu[MAXN][MAXN];
int x[100],y[100];
int hang[MAXN][MAXN];
int lie[MAXN][MAXN];
int ge[MAXN][MAXN];
int cnt,flag;
void print()
{
    int i,j;
    //printf("\n");
    for(i=0;i<9;++i)
    {
        for(j=0;j<9;++j)printf("%d",tu[i][j]);
        printf("\n");
    }
}
void dfs(int num)
{
    if(num==cnt)//如果填满了,输出即可
    {
        print();
        flag=1;
        return;
    }

    int nx=x[num];//空位置横坐标
    int ny=y[num];//空位置纵坐标
    int p=nx/3*3+ny/3;//此坐标所映射的九宫格坐标
    int i;
    for(i=1;i<=9;++i)//枚举1-9个数字
    {
        if(!hang[i][nx]&&!lie[i][ny]&&!ge[i][p])//如果这个位置可以放下这个数字
        {   
            //更新状态
            hang[i][nx]=1;
            lie[i][ny]=1;
            ge[i][p]=1;                      
            tu[nx][ny]=i;                   
            dfs(num+1);   //搜索下一个位置                   
            if(flag)return;
            //回溯
            hang[i][nx]=0;
            lie[i][ny]=0;            
            ge[i][p]=0;
            tu[nx][ny]=0;
        }
    }
}
int main()
{
    int t,key;
    int i,j;
    scanf("%d",&t);
    while(t--)
    {
        //初始化
        memset(hang,0,sizeof(hang));
        memset(lie,0,sizeof(lie));
        memset(ge,0,sizeof(ge));
        cnt=0;
        for(i=0;i<9;++i)
        {
            scanf("%s",tu[i]);
            for(j=0;j<9;++j)
            {
                tu[i][j]-='0';
                key=tu[i][j];
                //如果是已给出的数字,就标记状态
                if(key)
                {
                    hang[key][i]=1;
                    lie[key][j]=1;
                    ge[key][i/3*3+j/3]=1;
                }
                else//否则记录空位置坐标
                {
                    x[cnt]=i;
                    y[cnt++]=j;
                }
            }
        }
        flag=0;
        dfs(0);//从第一个空位置开始
    }
    return 0;
}

对于这个映射九宫格坐标,我们知道一张图有9个九宫格,一个九宫格对应9个小格,我们把九宫格从上到下,从左到右依次编号0-8,然后类比我们的二维数组,比如说a[3][3],实际上有3*3=9个空间,而要想得到这个编号就需要x*3+y,具体还是需要大家自己想想哒。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值