POJ 2676Sudoku(数独)

来源:互联网 发布:西澳大学知乎 编辑:程序博客网 时间:2024/05/08 00:38


Sudoku
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14110 Accepted: 6968 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

1103000509002109400000704000300502006060000050700803004000401000009205800804000107

Sample Output

143628579572139468986754231391542786468917352725863914237481695619275843854396127


题目链接:http://poj.org/problem?id=2676


暴力搜素居然过了,用数组zu,hang,shu分别表示每一组、每一行、每一列的状态。例如zu[i][j]=1;代表第i组中j已经使用过了,再例如hang[i][j]=0;代表第i行中j没有使用过。

数组b[i][j]表示第i行第j列的状态,0是未填,1是已填。

用回溯法填,用pd1表示是否填满,1代表未填满,0代表填满。填满后令pd1=0,退出循环,输出答案。


#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<algorithm>#include<math.h>#include<queue>using namespace std;char s[10][10];int a[10][10],b[105];bool zu[10][10],hang[10][10],shu[10][10],pd1;void f(int i,int j){    for(int t=1;t<=9&&pd1;t++){        if(zu[3*((i-1)/3)+(j-1)/3+1][t]==0&&hang[i][t]==0&&shu[j][t]==0){            int k;            a[i][j]=t;            zu[3*((i-1)/3)+(j-1)/3+1][t]=1;            hang[i][t]=1;            shu[j][t]=1;            int pd=0;            for(k=9*(i-1)+j+1;k<=81;k++){                if(b[k]==0)                {pd=1;break;}            }            if(pd){                f((k+8)/9,(k-1)%9+1);                if(pd1){                    zu[3*((i-1)/3)+(j-1)/3+1][t]=0;                    hang[i][t]=0;                    shu[j][t]=0;                }            }            else {                pd1=0;                return;            }        }    }    return ;}int main(){    int t,k;    cin>>t;    while(t--){        pd1=1;        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        memset(zu,0,sizeof(zu));        memset(hang,0,sizeof(hang));        memset(shu,0,sizeof(shu));        for(int i=1;i<=9;i++)            scanf("%s",s[i]);        for(int i=1;i<=9;i++)        for(int j=1;j<=9;j++){            a[i][j]=s[i][j-1]-'0';            if(a[i][j]!=0){                zu[3*((i-1)/3)+(j-1)/3+1][a[i][j]]=1;                hang[i][a[i][j]]=1;                shu[j][a[i][j]]=1;                b[9*(i-1)+j]=1;            }        }        for(k=1;k<=81;k++){            if(b[k]==0)                break;        }        f((k+8)/9,(k-1)%9+1);        for(int i=1;i<=9;i++){            for(int j=1;j<=9;j++){                printf("%d",a[i][j]);            }            cout<<endl;        }    }    return 0;}


0 0
原创粉丝点击