POJ 2676-Sudoku(抽象,回溯)

题目链接Sudoku

题目:

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

题意:

以前从来没有做过数独,也不知道数独是啥,没想到今天竟然用程序解决了一般数独的填写,啧啧,小有成就感;
这道题就是说让你输入一个9X9的数字字符串矩阵,每个小格子中填写十进制数1-9中的任意一个,空的格子用0表示。要求在空的格子上填写1-9中的任意一个数字,使每一行,每一列,以及每个3X3的矩阵中,都只由1-9的九个数字组成,不能有重复;我想这是属于简单的数独吧…

思路:

老规矩,row:行,line:列,square:3X3的矩阵;用这三个数组来表示数字是否出现过;举个例子:row[x][i]=1;表示数字i在x行已经出现过,标记为真值1;
dfs()关于参数的变化,谁变就让谁作为参数,这里主要是矩阵中数的位置的变化,那么就是从第一行第一列开始dfs(1,1);

AC代码:
#include <bits/stdc++.h>

using namespace std;
int mapp[10][10];
bool flag;
bool line[10][10]; //列
bool row[10][10]; //行
bool square[10][10]; //3x3的方框
void dfs(int x, int y) {
    if(x == 10) {
        flag = true;
        for(int i=1;i<=9;i++)//注意这个循环输出写在这里非常的重要!!!
        {
            for(int j=1;j<=9;j++)
                cout <<mapp[i][j];
            cout <<endl;
        }
        return;
    }
    if(flag) return;//如果已经有一种结果满足情况了,那么下一次的dfs到这里就可以终止了;
    if(mapp[x][y] == 0) {
        int k = 3*((x-1)/3)+(y-1)/3+1;
        for(int i = 1; i <= 9; i++) {
            if(!row[x][i]&&!line[y][i]&&!square[k][i]) {
                mapp[x][y] = i;
                row[x][i] = true;
                line[y][i] = true;
                square[k][i] = true;
                if(y == 9) dfs(x+1, 1);
                else dfs(x, y+1);

                mapp[x][y] = 0;//假设这个地方不放数字i,而是尝试其他数字(回溯)
                row[x][i] = false;
                line[y][i] = false;
                square[k][i] = false;
            }
        }
    }
    else {
        if(y == 9) dfs(x+1, 1);
        else dfs(x, y+1);
    }
}
int main() {
    int n;
    cin >>n;
    while(n--) {
        flag = false;
        memset(line, false, sizeof(line));
        memset(row, false, sizeof(row));
        memset(square, false, sizeof(square));
        for(int i = 1; i <= 9; i++) {
            for(int j = 1; j <= 9; j++) {
                char ss;
                cin >>ss;
                mapp[i][j] = ss-'0';//将输入的字符转化为十进制数字
                if(mapp[i][j]) {
                    int k = 3*((i-1)/3)+(j-1)/3+1;//将9x9矩阵划分为9个3x3矩阵
                    row[i][mapp[i][j]] = true;//表示在x行这个数mapp[i][j]已经出现过
                    line[j][mapp[i][j]] = true;
                    square[k][mapp[i][j]] = true;//表示在第k个矩阵这个数已经出现过
                }
            }
        }
        dfs(1,1);//从第一行第一列开始
    }
    return 0;
}

在这里插入图片描述
在这里有一个很有趣的地方,我开始的时候由于dfs的过程不清楚,把这个循环输出写在了main函数里面,所以结果一直都不对,但是dfs的过程的确是写对了,参考网上大佬的写法以后,找到了原因。当dfs的过程中,如果达到了满足的条件时会return,但是这个函数并没有结束,因为还有其他的分支(dfs)没有结束,虽然这里用了flag做了标记,但是由于没有进入下一次dfs之前,下面的for循环仍然在进行,所以导致mapp矩阵的值依旧在改变,所以应当在第一次达到符合条件时就把结果输出!

综上所述,以后输出结果尽量在dfs函数里面,以免出现错误。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值