poj2676 Sudoku

来源:互联网 发布:林弯弯网店淘宝网址 编辑:程序博客网 时间:2024/05/21 17:09


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

1103000509002109400000704000300502006060000050700803004000401000009205800804000107

Sample Output

143628579572139468986754231391542786468917352725863914237481695619275843854396127

题意就是输入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,具体还是需要大家自己想想哒。

1 0
原创粉丝点击