将9x9 2d阵列分成9个子网格(像数独)? (C ++) [英] Dividing a 9x9 2d array into 9 sub-grids (like in sudoku)? (C++)

查看:301
本文介绍了将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语句, 2(行从0开始)& col < 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天全站免登陆