【详解】Sudoku POJ - 2676(经典问题 DFS 重复剪枝)⭐⭐⭐

Sudoku POJ - 2676

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.

Examples

Sample Input
1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
Sample Output
143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

Hint




题意:

就是给出一个数独求解了, 行列, 每个3*3格子内的元素不能重复

题解:

数独这个问题太经典了, 简直就是为重复性剪枝而设立的一个问题.
我们设立3个数组, row, col, grid表示该行/列/小方格内某数是否已经出现.

首先我们要对上面3个数组进行初始化
然后我们要定义好我们的搜索顺序. 我们就朴素的从左向右, 从上向下搜就好了. 也就是y在外x在内的嵌套循环.
但是我们没有必要在Dfs里面写一个2层循环的, 因为这样的话就完全失去了剪枝的效果, 我们的目标只是对剪枝后的, 也就是有效的状态进行搜索.
这样的话我们每次搜索有效解时优先y+1, y==9再从(x+1, 1)开始

        if(y==9) flag = Dfs(x+1, 1); //搜索顺序
        else flag = Dfs(x, y+1);

到这里这道题目已经可以写了, 但其实对于数独问题还有更深的剪枝策略, 比方说我们改变搜索顺序, 优先从影响状态大的区域开始(也就是放的方格很多的行/列/小方格).

经验小结:

写Dfs别老盯着大循环, 老循环是很慢的


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const int inf = 1<<30;
const LL maxn = 11;

int maze[maxn][maxn];
bool grid[maxn][maxn];                //9个数字在每个3*3方格里的出现次数
bool row[maxn][maxn], col[maxn][maxn];//9个数字在每行/列里的出现次数
bool Dfs(int x, int y){
    if(x==10) return true;
    bool flag = false;
    if(maze[x][y]){                  //如果该点已有数
        if(y==9) flag = Dfs(x+1, 1); //搜索顺序
        else flag = Dfs(x, y+1);
        return flag;
    }

    int g = 3*((x-1)/3)+(y-1)/3+1;
    for(int i = 1; i <= 9; i++){
        if(!row[x][i] && !col[y][i] && !grid[g][i]){
            row[x][i] = col[y][i] = grid[g][i] = true;
            maze[x][y] = i;
            if(y==9) flag = Dfs(x+1, 1);
            else flag = Dfs(x, y+1);
            if(flag) return true;     //找到了解
            maze[x][y] = 0;
            row[x][i] = col[y][i] = grid[g][i] = false;//回溯
        }
    }
    return false;
}
int main()
{
    int T;
    cin >> T;
    while(T--){
        ms(grid, 0); ms(row, 0); ms(col, 0);
        char input;
        for(int i = 1; i <= 9; i++)
            for(int j = 1; j <= 9; j++){
                cin >> input;
                maze[i][j] = input-'0';
                if(maze[i][j]){
                    int g = 3*((i-1)/3)+(j-1)/3+1;
                    grid[g][maze[i][j]] = true;      //初始化剪枝数组
                    row[i][maze[i][j]] = true;
                    col[j][maze[i][j]] = true;
                }
            }
        Dfs(1, 1);
        for(int i = 1; i <= 9; i++){
            for(int j = 1; j <= 9; j++){
                cout << maze[i][j];
            }
            cout << endl;
        }
    }
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值