c++ - 确定数独板是否有效

标签 c++ algorithm sudoku

我遵循了 Adnan Aziz 等人的 Elements of Programming Interviews C++ 中的解决方案。他们有这个解决方案来确定数独板是否有效。

#include <iostream>
#include <vector>
#include <utility>
#include <cmath>
#include <deque>

using namespace std;

bool HasDuplicate(vector<vector<char>>& partial_assignment, int start_row, int end_row, int start_col, int end_col);

bool sudokuSolve(vector<vector<char>>& Board)
{
    // your code goes here
    // Check the row constraint

    // Check row constraint
    for (int i = 0; i < Board.size(); ++i)
    {
        if (HasDuplicate(Board, i, i + 1, 0, Board.size()))
            return false;
    }

    // Check column constraint
    for (int j = 0; j < Board.size(); ++j)
    {
        if (HasDuplicate(Board, 0, Board.size(), j, j + 1))
            return false;
    }

    // Check regional constraints
    int region_size = (int)sqrt(Board.size());
    for (int i = 0; i < region_size; ++i)
    {
        for (int j = 0; j < region_size; ++j)
        {
            if (HasDuplicate(Board, region_size * i, region_size * (i + 1), region_size * j, region_size * (j + 1)))
                return false;
        }
    }
    return true;
}

bool HasDuplicate(const vector<vector<int>>& partial_assignment, int start_row,
    int end_row, int start_col, int end_col)
{
    // this creates a container for bookkeeping of used numbers
    // size+1 because the number 1-x are used.
    deque<bool> is_present(size(partial_assignment) + 1, false);

    // The variables i and j are used to go through every coordinate on the
    // sudoku game board.
    for (int i = start_row; i < end_row; ++i)
    {
        for (int j = start_col; j < end_col; ++j)
        {
            // here it checks if the current number is already marked as used in "is_present"
            // if it is, then it's a duplicate and the function returns true.

            // The value 0 is used at coordinates where no number has been
            // selected.
            if (partial_assignment[i][j] != 0 && is_present[partial_assignment[i][j]])
                return true;

            // otherwise, mark the number as used
            is_present[partial_assignment[i][j]] = true;
        }

    }
    return false;
}

int main() {

    std::vector<std::vector<char>> board = {
    {'5','3','.','.','7','.','.','.','.'},
    {'6','.','.','1','9','5','.','.','.'},
    {'.','9','8','.','.','.','.','6','.'},
    {'8','.','.','.','6','.','.','.','3'},
    {'4','.','.','8','.','3','.','.','1'},
    {'7','.','.','.','2','.','.','.','6'},
    {'.','6','.','.','.','.','2','8','.'},
    {'.','.','.','4','1','9','.','.','5'},
    {'.','.','.','.','8','.','.','7','9'}
    };

    std::cout << sudokuSolve(board) << "\n";
    return 0;
}

不幸的是,作者提供的代码似乎有一些错误。当我收到这些无法修复的错误时:

1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1>Source.obj : error LNK2019: unresolved external symbol "bool __cdecl HasDuplicate(class std::vector<class std::vector<char,class std::allocator<char> >,class std::allocator<class std::vector<char,class std::allocator<char> > > > &,int,int,int,int)" (?HasDuplicate@@YA_NAAV?$vector@V?$vector@DV?$allocator@D@std@@@std@@V?$allocator@V?$vector@DV?$allocator@D@std@@@std@@@2@@std@@HHHH@Z) referenced in function "bool __cdecl sudokuSolve(class std::vector<class std::vector<char,class std::allocator<char> >,class std::allocator<class std::vector<char,class std::allocator<char> > > > &)" (?sudokuSolve@@YA_NAAV?$vector@V?$vector@DV?$allocator@D@std@@@std@@V?$allocator@V?$vector@DV?$allocator@D@std@@@std@@@2@@std@@@Z)
1>  Hint on symbols that are defined and could potentially match:
1>    "bool __cdecl HasDuplicate(class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > const &,int,int,int,int)" (?HasDuplicate@@YA_NABV?$vector@V?$vector@HV?$allocator@H@std@@@std@@V?$allocator@V?$vector@HV?$allocator@H@std@@@std@@@2@@std@@HHHH@Z)
1>C:\Dev\Test\Debug\Test.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

谁能发现错误?

最佳答案

你在开头有一个声明:

bool HasDuplicate(vector<vector<char>>& partial_assignment, int start_row, int end_row, int start_col, int end_col);

和下面的实际函数(具有不同的签名):

bool HasDuplicate(const vector<vector<int>>& partial_assignment, int start_row, int end_row, int start_col, int end_col)

您可以决定 vectorint 还是 char 类型并相应地进行修复。

关于c++ - 确定数独板是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58812491/

相关文章:

c++ - 以预定义的非均匀概率生成范围内的随机数

algorithm - 寻找成对的集合,使它们的并集具有特定的大小

python - 数独消除策略

algorithm - 数独题的生成

c++ - 为什么不应该以这种方式隐藏结构实现?

c++列表迭代器的段错误

c++ - 二维数组— “Too many initializer values”警告/错误

python - 绘制n个相同的无重叠和中心重心的圆

algorithm - 带有扭曲的图形遍历算法 - 最小停止次数

算法 X 解决精确覆盖 : Fat Matrices