数独解算器不工作(回溯、前向检查、C++)

问题描述 投票:0回答:1

我正在研究优化的数独求解器。尽管我添加了前向检查,但回溯的基本思想仍然存在。为每个单元格存储域(可以在单元格中设置哪些数字)。在尝试将数字设置到单元格中并在不起作用时回溯之前,我会检查所有相关单元格以查看该数字是否是唯一可以在那里设置的数字。如果发生冲突,我会尝试下一个数字,依此类推。最理想的是,这减少了必要的回溯量。

但是,不幸的是我的代码无法正常工作。我将非常感谢您的帮助。

致以诚挚的问候

埃里克

代码:

#include <iostream>
#include <bitset>
#include <array>
using namespace std;

#define n 9

void print_board(int board[n][n]);
bool solve(array<bitset<n>, n * n> &domains, int board[n][n], int i = 0, int j = 0);
void initialize_domains(array<bitset<n>, n * n> &domains, int board[n][n]);
bool update_domains(array<bitset<n>, n * n> &domains, int board[n][n], int i, int j, int k);

std::bitset<n> reverse_bits(const std::bitset<n> &bitset)
{
    std::bitset<n> reversed;

    for (int i = 0; i < n; ++i)
    {
        reversed[i] = bitset[n - 1 - i]; // Sets the i-th bit in reversed to the (N-1-i)-th bit in bitset
    }

    return reversed;
}

void printDomains(const std::array<std::bitset<n>, n * n> &domains)
{
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            std::cout << "Domain for cell (" << i << ", " << j << "): " << reverse_bits(domains[i * n + j]) << std::endl;
        }
    }
}

int main() // main function handles the flow of the program
{
    int board[n][n] = {// definition of the initial board
                       {5, 3, 0, 0, 7, 0, 0, 0, 0},
                       {6, 0, 0, 1, 9, 5, 0, 0, 0},
                       {0, 9, 8, 0, 0, 0, 0, 6, 0},
                       {8, 0, 0, 0, 6, 0, 0, 0, 3},
                       {4, 0, 0, 8, 0, 3, 0, 0, 1},
                       {7, 0, 0, 0, 2, 0, 0, 0, 6},
                       {0, 6, 0, 0, 0, 0, 2, 8, 0},
                       {0, 0, 0, 4, 1, 9, 0, 0, 5},
                       {0, 0, 0, 0, 8, 0, 0, 7, 9}};

    std::array<std::bitset<n>, n * n> domains;

    initialize_domains(domains, board);
    print_board(board);
    solve(domains, board);
    cout << "\n";
    print_board(board);
}

void print_board(int board[n][n]) // displays the Sudoku board
{
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            cout << board[i][j] << " ";
        }
        cout << "\n";
    }
}

// starting from the cell of a cell, update the domains of the influenced cells
bool update_domains(std::array<std::bitset<n>, n * n> &domains, int board[n][n], int i, int j, int k)
{
    for (int a = 0; a < n; ++a)
    {
        domains[i * n + a].set(k - 1); // row
        domains[j + n * a].set(k - 1); // column

        // If it's no longer possible to place a number in the cell
        if (domains[i * n + a].all() && board[i][a] == 0)
            return false;
        if (domains[j + n * a].all() && board[a][j] == 0)
            return false;
    }

    for (int row = (i / 3) * 3; row < ((i / 3) * 3) + 3; ++row)
    {
        for (int col = (j / 3) * 3; col < ((j / 3) * 3) + 3; ++col)
        {

            domains[row * n + col].set(k - 1); // subgrid

            if (domains[row * n + col].all() && board[row][col] == 0)
                return false;
        }
    }

    return true;
}

// 0 = possible to place the number
// 1 = not possible to place the number
void initialize_domains(array<bitset<n>, n * n> &domains, int board[n][n])
{
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            if (board[i][j] != 0)
                update_domains(domains, board, i, j, board[i][j]);
        }
    }
}

// i specifies the row
// j specifies the column
bool solve(array<bitset<n>, n * n> &domains, int board[n][n], int i, int j) // responsible for solving the Sudoku using brute-force backtracking
{
    if (i == n) // if all rows and thus all columns are successfully filled, the Sudoku is solved
        return true;
    if (j == n) // if the column index exceeds the dimension, the first cell of the next row must be considered
        return solve(domains, board, i + 1, 0);
    if (board[i][j] != 0) // if there is no empty cell at the current position, move to the next position
        return solve(domains, board, i, j + 1);

    for (int k = 1; k <= n; ++k) // all valid numbers from 1-9 are tried linearly
    {
        if (domains[i * n + j].test(k - 1)) // if the number is not in the domain of the current cell, it can be skipped
            continue;

        array<bitset<n>, n * n> pre_updated_domain = domains;

        if (update_domains(domains, board, i, j, k)) // no conflict
        {
            board[i][j] = k; // the supposedly valid number is tried

            if (solve(domains, board, i, j + 1)) // if no further conflicts arise, this is the correct solution
                return true;

            board[i][j] = 0;
        }

        // if the number leads to a conflict (in an influenced cell, no number can be placed anymore), it will not be used
        domains = pre_updated_domain;
    }

    return false; // if none of the n numbers is valid, a conflict has occurred
}

我目前的结果如下:

5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9


5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9


我预计:
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9

c++ recursion backtracking sudoku bitset
1个回答
1
投票

主要问题在于如何实现回溯。为了回溯,您可以使用

board[i][j] = 0;
正确撤消移动,但不会撤消移动对
domains
产生的影响。这意味着你只会减少
domains
允许的可能性,但在你收回一步后永远不会“打开”可能性。结果,你可能会拒绝实际上能带来解决方案的举措。

与您在

domains
上执行的更新一样,您会丢失之前的信息(很可能在调用
set(k - 1)
之前已经设置了设置位 - 或者没有,您不知道),编写一个
undo
函数来将这些位恢复到之前的状态并不是一种简单的方法。

要解决此问题,您可以例如跟踪某个单元格不再可用的数字,并且对每个单元格进行计数,以便了解之前有多少次(最多 3 次)移动使该数字变得不可能。然后,当回溯时,您可以减少该计数器。只有当它变为 0 时,该数字才再次成为该单元格的可行可能性。

© www.soinside.com 2019 - 2024. All rights reserved.