确定数独板是否有效
Determining whether a sudoku board is valid
我遵循了Adnan Aziz等人的编程元素访谈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:DevTestDebugTest.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)
您可以决定vector
的类型是int
还是char
,并进行相应的修复。
更正后的代码为.
#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)
{
// this creates a container for bookkeeping of used numbers
// size+1 because the number 1-x are used.
deque<bool> is_present(partial_assignment.size() + 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;
}
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;
}
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.更改了HasDuplicate签名,如果你在一个文件中写作,就不用提前声明了。
相关文章:
- 如果变量名称不跟在 char* 后面,const char* 是否有效?
- 钳制迭代器是否有效
- 检查由括号、方括号和大括号组成的一组方括号是否有效?
- 在函数内创建的对象的范围 - 如果在函数外部存储和访问引用,它们是否有效?
- 模板签名解析为 void(void) 被 GCC 拒绝;这是否有效C++?
- 我如何知道作为参数的size_t在函数中是否有效?
- 我的运算符重载是否有效<<(流插入)左操作数不是 ostream
- C++ 返回指向函数内定义的静态数组的指针是否有效?
- 此递归模板类型定义是否有效C++?
- 将 C 函数转换为 C++ 以检查数字是否有效
- 函数参数的名称与调用函数时使用的变量相同是否有效?
- 如何检查输入是否有效?
- 如何检查用户的输入是否有效以及我正在寻找的数字?
- 在函数中按值传递 unordered_map/unordered_set 是否有效? C++
- 如何检查isupper(cstr)是否有效?
- 将 std::transform 与 std::back_inserter 一起使用是否有效?
- 只需要知道我在c ++中打印模式的方式是否有效,或者有另一种方法可以有效地做到这一点
- 如何检查字符串格式在读取C++文本文件时是否有效?
- 在 c++ 中将对象设置为等于同一类的构造函数是否有效?
- 创建加密安全密码.并验证它是否有效