Dividing a 9x9 2d array into 9 sub-grids (like in sudoku)? (C++)

后端 未结 3 1082
独厮守ぢ
独厮守ぢ 2020-12-10 23:23

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 solu

相关标签:
3条回答
  • 2020-12-10 23:52

    I would create a lookup table with 81 entrys. Each entry refers to a cell in the 9x9 grid and gives you the information you need (which box, which column, which row, ...)

    0 讨论(0)
  • 2020-12-11 00:03

    I use this myself (but then in python, assuming x and y go from 0 to 9 exclusive):

    int bx, by;
    for (bx = (x/3)*3; bx < (x/3)*3 + 3; bx++) {
        for (by = (y/3)*3; by < (y/3)*3 + 3; by++) {
            // bx and by will now loop over each number in the block which also contains x, y
        }
    }
    
    0 讨论(0)
  • 2020-12-11 00:12

    You could calculate a block number from row and column like this:

    int block = (row/3)*3 + (col/3);
    

    This numbers the blocks like this:

    +---+---+---+
    | 0 | 1 | 2 |
    +---+---+---+
    | 3 | 4 | 5 |
    +---+---+---+
    | 6 | 7 | 8 |
    +---+---+---+
    
    0 讨论(0)
提交回复
热议问题