将 9x9 2d 数组划分为 9 个子网格(如数独)?(C++) [英] Dividing a 9x9 2d array into 9 sub-grids (like in sudoku)? (C++)

查看:26
本文介绍了将 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++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆