POJ 2676 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

分析

题目大意就是玩数独游戏,横向、纵向以及数字所在的3x3小宫格都要是1-9并且不能重复,没填好的空格子输入的数字中是0。
解题思路:记录下所有未填的空格,每个空格子枚举9个数字,深搜尝试所有可能性即可。需要关注是计算宫格序号的方法i/3 * 3+j/3(i, j >= 0) 。写此题时想了下用一个数组标记搜索状态。
试验时先开始从第一个开始填,后来又尝试从最后一个开始填,结果顺序填700多ms,倒序0ms。。,差距太大了,于是下面列的是倒序填的方法。

实现

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
#define N 9
int a[N][N];
//visited[i][j][k]: i为0表示行,为1表示列,2为九宫格,如visited[0][3][9]=1表示第3行已放了9。
int visited[3][10][10];

struct Pos
{
    int r, c;
    Pos(int row, int column) : r(row), c(column) {}
};

//保存所有未填数字的位置。
vector<Pos> blanks;

inline bool isVisited(int i, int j, int number)
{
    return visited[0][i][number] || visited[1][j][number] || visited[2][i / 3 * 3 + j / 3][number];
}

inline void setVisited(int i, int j, int number, int flag)
{
    visited[0][i][number] = flag;
    visited[1][j][number] = flag;
    visited[2][i / 3 * 3 + j / 3][number] = flag;
}


void print()
{
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << a[i][j];
        }
        cout << endl;
    }
}

//从第index数开始填
bool dfs(int index)
{
    if (index < 0) {
        return true;
    }
    int r = blanks[index].r;
    int c = blanks[index].c;
    for (int i = 1; i <= N; i++) {
        if (!isVisited(r, c, i) && a[r][c] == 0) {
            a[r][c] = i;
            setVisited(r, c, i, 1);
            if (dfs(index - 1)) {
                return true;
            }
            //后续的点没放成功则回溯。
            a[r][c] = 0;
            setVisited(r, c, i, 0);
        }
    }
    return false;
}

int main()
{
//  freopen("in.txt", "r", stdin);
    int cases;
    cin >> cases;
    while (cases--) {
        memset(visited, 0, sizeof(visited));
        blanks.clear();
        char ch;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                cin >> ch;
                a[i][j] = ch - '0';
                if (a[i][j]) {
                    setVisited(i, j, a[i][j], 1);
                }
                else {
                    blanks.push_back(Pos(i, j));
                }
            }
        }
        dfs(blanks.size() - 1);
        print();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值