POJ - 2676 Sudoku 题解

Sudoku

Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

Submit Status

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即可,但是要做预处理,博主方法还是很好的
我们把数独分成3*3的9块,编号1—9
re_fang[i][j]=k 代表位置(i,j)所在方块(其实可以用方法直接计算
re_fang[i][j]=i*3+(j-1)/3+1)
lie[i][j]=1/0 代表第i列有没有出现k
hang[i][j]=k 同理
不要忘了初始化就行
代码如下:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<math.h>
using namespace std;
int re_fang[10][10]={0,0,0,0,0,0,0,0,0,0,
                                 0,1,1,1,2,2,2,3,3,3,
                                 0,1,1,1,2,2,2,3,3,3,
                                 0,1,1,1,2,2,2,3,3,3,
                                 0,4,4,4,5,5,5,6,6,6,
                                 0,4,4,4,5,5,5,6,6,6,
                                 0,4,4,4,5,5,5,6,6,6,
                                 0,7,7,7,8,8,8,9,9,9,
                                 0,7,7,7,8,8,8,9,9,9,
                                 0,7,7,7,8,8,8,9,9,9,};
bool fang[12][12];
bool hang[12][12];
bool lie[12][12];
int arr[12][12];
int f=0;
int dfs(int x,int y)
{
        if (f==1)       return 0;
        if (x==10)
        {
                f=1;
                for (int i=1;i<=9;i++)
                {
                        for (int j=1;j<=9;j++)
                                cout<<arr[i][j];
                        cout<<endl;
                }
        }
        if (arr[x][y]!=0)
        {
                if (y==9)
                        dfs(x+1,1);
                else dfs(x,y+1);
                return 0;
        }
        for (int i=1;i<=9;i++)
                if (fang[re_fang[x][y]][i]==0 && lie[y][i]==0 && hang[x][i]==0)
                {
                        fang[re_fang[x][y]][i]=1;
                        lie[y][i]=1;
                        hang[x][i]=1;
                        arr[x][y]=i;
                        if (y==9)
                                dfs(x+1,1);
                        else dfs(x,y+1);
                        fang[re_fang[x][y]][i]=0;
                        lie[y][i]=0;
                        hang[x][i]=0;
                        arr[x][y]=0;
                }
}



int main()
{
        int t;
        scanf("%d",&t);
        char cc;
        while(t--)
        {
                memset(arr,0,sizeof(arr));
                memset(fang,0,sizeof(fang));
                memset(lie,0,sizeof(lie));
                memset(hang,0,sizeof(hang));
                for (int i=1;i<=9;i++)
                        for (int j=1;j<=9;j++)
                        {
                                cin>>cc;
                                arr[i][j]=cc-'0';
                                if (arr[i][j]!=0)
                                {
                                        fang[re_fang[i][j]][arr[i][j]]=1;
                                        lie[j][arr[i][j]]=1;
                                        hang[i][arr[i][j]]=1;
                                }
                        }
                f=0;
                dfs(1,1);
        }
}


转载请点赞,谢谢
--------


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值