开发者

Sudoku - find current box(square or rectangle) based on row, col, dimension(?) and box-size

Disclaimer: Found the topic with squared boxes, and the algorithms there only works for that specific problem.

I'm making a recursi开发者_运维知识库ve backtracking sudoku solver, but I'm having trouble assigning which box is related to a given cell.

Let's say we've got a 4x4 board with 2x2-cells-sized boxes. A fitting algorithm would then be (from related topic):

int numMajorRows = 2;
int numMajorCols = 2;  
int width = 2;

// assuming row and col also start at 1.  
int squareNumber(int row, int col) {
  int majorRow = (row-1) / width;  // zero based majorRow
  int majorCol = (col-1) / width;  // zero based majorCol
  return majorCol + majorRow * numMajorCols + 1;
}

However, what would the algorithm look like if say we have a 6x6 board with 2x3-cells-sized boxes? I just can not seem to figure it out..

Thanks in advance :-)

Edit: [Partially solved] I brute forced it a bit, with forcing the height of the box to always be larger than the width. Still, I'm interested in a nifty algorithm for this :)


The given algorithm only works for square boxes. Now you have a rectangular box and you'll need an extra height attribute (the given example algorithm reuses the width parameter where a cell height is needed - OK with squares, not OK with rectangles in general)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜