Sudoku POJ2676

题目:

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.

输入:

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.

输出:

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.

样例输入:

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

样例输出:

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

数独游戏,刚看到这个题的时候有点懵,但是其实这个题不是难题。

题意就是往这个格子里填数,这一个大网格分成九个小格,填数的时候要保证这个数存在的这一行,这一列,和这个方块没有这个数才行。需要建立几个数组,ark用来表示每个0存在的行数和列数。hang[i][j]表示在i行存在“j”这个数,则标记为1,lie[i][j]表示第i列存在“j”这个数,则标记为1。(这里的i,j只是形式参数,具体的看程序)。mapp这个数组用来存数。

check判断的是这个数存在不存在于它所属于的那个块中。格式为行j/3*3到j/3*3+2,列为k/3*3到k/3*3+2;这个表示方法很巧妙, 比如说这个数存在于第1,2,3行,对于数组来说对应的就是0,1,2,然后0,1,2,代入上面的式子,得到的正好是都是0到2的范围,这就是第一个块的范围,其余的块的范围确定是一样的。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
char ch[10];
int mapp[10][10];
int ark[100][3];
int hang[10][10];
int lie[10][10];
int check(int j,int k,int i)//检查块中是否有相同的数
{
    for(int p=j/3*3;p<=j/3*3+2;p++)
    {
        for(int q=k/3*3;q<=k/3*3+2;q++)
        {
            if(mapp[p][q]==i)
                return 0;
        }
    }
    return 1;
}
int dfs(int num)
{
    int j=ark[num][0];
    int k=ark[num][1];
    if(num<0) return 1;
    for(int i=0;i<9;i++)
    {
        if(check(j,k,i+1)&&!hang[j][i]&&!lie[k][i])
        {
            hang[j][i]=lie[k][i]=1;//如果满足条件,说明要填数,所以标记一下这一行这一列
            mapp[j][k]=i+1;//填数
            if(dfs(num-1))return 1;//我理解的是起到退出的作用,比如说进入第二层的时候num<0了,那么就return到dfs(num-1)这个地方为1,然后return 1退出。
            hang[j][i]=lie[k][i]=0;
            mapp[j][k]=0;
        }
    }
    return 0;
}
int main()
{
   int n;
   scanf("%d",&n);
   while(n--)
   {
       memset(hang,0,sizeof(hang));
       memset(lie,0,sizeof(lie));
       int num=0;
       for(int i=0;i<9;i++)
       {
           scanf("%s",ch);//先用字符输入,再转成数字
           for(int j=0;j<9;j++)
           {
               mapp[i][j]=ch[j]-'0';
               if(mapp[i][j]==0)
               {
                   ark[num][0]=i;//num记录0的个数,这两个数组分别记录它所在的行数和列数
                   ark[num][1]=j;
                   num++;
               }
               if(mapp[i][j]) hang[i][mapp[i][j]-1]=1;//记录第i行的数,之所以减1是因为格子里的数都是从1开始的,但是在dfs函数里面的for循环是从0开始的,所以要对应
           }
       }
       for(int j=0;j<9;j++)
       {
           for(int i=0;i<9;i++)
            if(mapp[i][j]) lie[j][mapp[i][j]-1]=1;//记录第j列的数,减1的理由同上
       }
       dfs(num-1);//记录完所有的0之后最后num多加了一次,所以要减1
       for(int i=0;i<9;i++)
       {
           for(int j=0;j<9;j++)
           {
               printf("%d",mapp[i][j]);
           }
           printf("\n");
       }
   }
   return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值