Dividing a 9x9 2d array into 9 sub-grids (like in sudoku)? (C++)(将 9x9 2d 数组划分为 9 个子网格(如数独)?(C++))
问题描述
我正在尝试编写数独求解器的代码,我尝试这样做的方法是拥有一个 9x9 的指针网格,其中包含拥有解决方案或有效可能值的设置"对象的地址.
I'm trying to code a sudoku solver, and the way I attempted to do so was to have a 9x9 grid of pointers that hold the address of "set" objects that posses either the solution or valid possible values.
我能够通过 2 个 for 循环遍历数组,首先遍历每一列,然后转到下一行并重复.
I was able to go through the array with 2 for loops, through each column first and then going to the next row and repeating.
但是,我很难想象如何指定特定单元格属于哪个子网格(或框、块等).我最初的印象是在 for 循环中有 if 语句,例如 if row <2 (行从 0 开始) &颜色 <2 然后我们在第一个街区,但这似乎变得混乱.有没有更好的方法来做到这一点?
However, I'm having a hard time imagining how I would designate which sub-grid (or box, block etc) a specific cell belongs to. My initial impression was to have if statements in the for loops, such as if row < 2 (rows start at 0) & col < 2 then we're in the 1st block, but that seems to get messy. Would there be a better way of doing this?
推荐答案
您可以像这样从行和列计算块编号:
You could calculate a block number from row and column like this:
int block = (row/3)*3 + (col/3);
这样对块进行编号:
+---+---+---+
| 0 | 1 | 2 |
+---+---+---+
| 3 | 4 | 5 |
+---+---+---+
| 6 | 7 | 8 |
+---+---+---+
这篇关于将 9x9 2d 数组划分为 9 个子网格(如数独)?(C++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 9x9 2d 数组划分为 9 个子网格(如数独)?(C++)
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01