POJ 2676 sudoku DFS

E - Sudoku
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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

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

思路:同一行、同一列我们大家应该都会去处理;用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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Marcus-Bao

万水千山总是情,只给五角行不行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值