用于检查我们是否有有效数独的辅助功能
Helper function for checking if we have a valid sudoku
我正在关注一本名为《编程访谈元素》的书,我正在阅读第 79 页上一个名为HasDuplicate
的辅助函数,但我很难理解它是如何工作的。
这是代码:
// Return true if subarray partial_assignment[start_row]
// [end_row - 1][start_col, end_col - 1] contains any duplicate
// in {1, 2 ..., size(partial_assignemnt)}; otherwise return false.
bool HasDuplicate(const vector<vector<int>>& partial_assignment, int start_row, int end_row, int start_col, int end_col)
{
deque<bool> is_present(size(partial_assignment) + 1, false);
for(int i = start_row; i < end_row; ++i)
{
for(int j = start_col; j < end_col; ++j)
{
if(partial_assignment[i][j] != 0 && is_present[partial_assignment[i][j]])
return true;
is_present[partial_assignment[i][j]] = true;
}
}
return false;
}
请注意,partial_assignment是部分填充的数独网格。我只是不确定它如何检查是否有重复项。也许它与德克有关?
代码中的注释:
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;
}
相关文章:
- 使用无符号字符数组有效存储内存
- 有效地计算多维数组的累积和?
- 计算数组重复次数的组合的有效算法,加起来达到给定的总和
- C++ 返回指向函数内定义的静态数组的指针是否有效?
- 错误:在尝试检测 std::cout 是否<< t 时,功能强制转换为数组类型;有效
- 声明后,gcc 的动态大小数组是否与标准数组有效相同?
- 如何有效地找到数组中三元组和的最小差异?
- 如何在C++中有效地将数字值重新分配给字符数组
- C++:功能在输出前自动使用50减/增加输入数
- 如何有效地收集给定数组中的重复元素?
- 如何实现删除数组的功能?
- 借助功能将 2D 转换为 1D 数组
- 用于检查我们是否有有效数独的辅助功能
- 编写一个递归功能,该功能采用数组并以相反顺序显示元素,而无需在末尾启动数组的索引
- 带有Boost Python的Wrap C 功能 - Numpy数组类型
- C 制造功能返回数组交叉文件
- 访问C 类中功能指针数组的输入
- 功能指针数组(包括成员功能)投掷模板专业化错误
- 为什么使用此已删除的功能有效
- 这是arduino和C++的有效数组吗