POJ 2676 sudoku DFS

来源:互联网 发布:随机算法原理 编辑:程序博客网 时间:2024/05/16 06:48
E - Sudoku
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
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

1103000509002109400000704000300502006060000050700803004000401000009205800804000107

Sample Output

143628579572139468986754231391542786468917352725863914237481695619275843

854396127

题意:数独填空,同一个数要求我们不放在同一行,同一列,同一个格子里;将空格补满;

思路:同一行、同一列我们大家应该都会去处理;用c[x][i]表示在该x列中是否出现过i,r[x][i]表示在第x行中是否出现

过该数字;book[x][i]表示在第x个格子中是否出现过该数字;

那么怎么表示第几个格子:我们推导出的公式为:3*(i-1)/3+(j-1)/3+1(i,j从1开始);剩下的就是普通的dfs问题了;

//还有我在处理该题的时候,由于有的格子上已经存在数字,我们需要的是对空白处填满数字,所以我就开了一个二维数组,

将所有需要填空的数字的坐标存在二维数组中(ans[][0],ans[][1]好比按方向搜索的方向)记录下一共需要填空的数目

;如果有数字那么久将该行该列该格子出现过的数就行标记,最后dfs就好了;

#include<stdio.h>#include<string.h>int book[10][10],c[10][10],r[10][10];int num[10][10],ans[100][10],count;int flag;void dfs(int k){    int j,tx,ty;     if(k<0)       {  flag=1;      return ;   }   tx=ans[k][0];   ty=ans[k][1];  for(j=1;j<=9;j++)               {  if(c[ty][j]==0&&r[tx][j]==0&&book[3*((tx-1)/3)+(ty-1)/3+1][j]==0)                      { num[tx][ty]=j;                      c[ty][j]=1;                      r[tx][j]=1;                      book[3*((tx-1)/3)+(ty-1)/3+1][j]=1;                      dfs(k-1);                      if(flag)                       return ;                      num[tx][ty]=0;                      c[ty][j]=0;                      r[tx][j]=0;                      book[3*((tx-1)/3)+(ty-1)/3+1][j]=0;                     }                                     }  return ;}int main(){    int t,i,j;     char a;    scanf("%d",&t);    getchar();    while(t--)    {        count=0;    flag=0;     memset(book,0,sizeof(book));         memset(r,0,sizeof(r));         memset(c,0,sizeof(c));     for(i=1;i<=9;i++,getchar())       for(j=1;j<=9;j++)         {  scanf("%c",&a);            num[i][j]=a-'0';            if(num[i][j]==0)            {  ans[count][0]=i;               ans[count][1]=j;               count++;//比较关键的地方,用这种方向代替了按方向去dfs} else{  int x=num[i][j]; r[i][x]=1; c[j][x]=1; book[3*((i-1)/3)+(j-1)/3+1][x]=1;} }         dfs(count-1);         for(i=1;i<=9;i++)         {   for(j=1;j<=9;j++)           printf("%d",num[i][j]);           printf("\n");   }         }return 0;}

1 0
原创粉丝点击