如何获得数独二维数组的索引,因为它与QUOT;分格"和"细胞内式分格"索引? [英] How to get the Sudoku 2D-array index, given its "sub-grid" and "cell-in-the-sub-grid" indexes?

查看:145
本文介绍了如何获得数独二维数组的索引,因为它与QUOT;分格"和"细胞内式分格"索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个数独的事情,我无法还原它的一个组成部分。

我设计一个 SudokuBoard 对象有一个基本的二维字节数组,和三种不同的观点,即数组:行单位,列单位和网格单元。网格是九经九块,像这样的索引:

 网​​格指数

        | |
   0 | 1 | 2
        | |
 -------------------------
        | |
   3 | 4 |五
        | |
 -------------------------
        | |
   6 | 7 | 8
        | |
 

如果每个网格有九个单元,这样的索引

 细胞指数

   0 1 2
   3 4 5
   6 7 8
 

下面是二维数组坐标板:

  2D-ARRAY坐标

[0,0] [0,1] [0,2] | [0,3] [0,4] [0,5] | [0,6] [0,7] [0,8]
[1,0] [1,1] [1,2] | [1,3] [1,4] [1,5] | [1,6] [1,7] [1,8]
[2,0] [2,1] [2,2] | [2,3] [2,4] [2,5] | [2,6] [2,7] [2,8]
------------------- + ------------ + -------- -----------
[3,0] [3,1] [3,2] | [3,3] [3,4] [3,5] | [3,6] [3,7] [3,8]
[4,0] [4,1] [4,2] |并[4,3] [4,4] [4,5] | [4,6] [4,7] [4,8]
[5,0] [5,1] [5,2] | [5,3] [5,4] [5,5] | [5,6] [5,7] [5,8]
------------------- + ------------ + -------- -----------
[6,0] [6,1] [6,2] | [6,3] [6,4] [6,5] | [6,6] [6,7] [6,8]
[7,0] [7,1] [7,2] | [7,3] [7,4] [7,5] | [7,6] [7,7] [7,8]
[8,0] [8,1] [8,2] | [8,3] [8,4] [8,5] | [8,6] [8,7] [8,8]
 

我创建了一个函数来获取数组坐标,给电网和细胞指标。确定列索引是一个脑克星,但我理解了它是

  INT colIdx =(cell_idx%3)+((grid_idx%3)* 3);
 

我也想通了,做决定行的索引,但我不满意怎么复杂,这就是。

  INT rowIdx = -1;
如果(grid_idx 3;){
   rowIdx =((cell_idx&所述; 3)0
      :((cell_idx&10 6)1:2));
}否则如果(grid_idx< 6){
   rowIdx =((cell_idx&所述; 3)3
      :((cell_idx&10 6)4:5));
}  其他  {
   rowIdx =((cell_idx&所述; 3)6
      :((cell_idx< 6)7:8));
}
 

如果任何人有关于如何减少这种,理想情况下,以一个工科数学公式的想法,我倒是AP preciate吧。

下面的演示它的工作程序。感谢您的帮助。

 进口java.util.Arrays中;
/ **
   < P> {@ code SudokuGridUnitCoordinates}< / P>
 ** /
公共类SudokuGridUnitCoordinates {
   公共静态最终无效的主要(字符串[] idxZeroGridIdx_0to8){
     INT gridIdx = -1;
     尝试  {
        gridIdx =的Integer.parseInt(idxZeroGridIdx_0to8 [0]);
        如果(gridIdx℃,|| gridIdx→8){
           抛出新抛出:IllegalArgumentException();
        }
     }赶上(抛出:IllegalArgumentException | ArrayIndexOutOfBoundsException异常X){
        抛出新抛出:IllegalArgumentException(通过8 idxZeroGridIdx_0to8的第一个元素必须是数字零:+ Arrays.toString(idxZeroGridIdx_0to8));
     }

     printCellCoordinates(gridIdx,0);
     printCellCoordinates(gridIdx,1);
     printCellCoordinates(gridIdx,2);
     printCellCoordinates(gridIdx,3);
     printCellCoordinates(gridIdx,4);
     printCellCoordinates(gridIdx,5);
     printCellCoordinates(gridIdx,6);
     printCellCoordinates(gridIdx,7);
     printCellCoordinates(gridIdx,8);
  }
  私有静态最终无效printCellCoordinates(INT grid_idx,诠释cell_idx){
     INT rowIdx = -1;
     如果(grid_idx 3;){
        rowIdx =((cell_idx&所述; 3)0
           :((cell_idx&10 6)1:2));
     }否则如果(grid_idx< 6){
        rowIdx =((cell_idx&所述; 3)3
           :((cell_idx&10 6)4:5));
     }  其他  {
        rowIdx =((cell_idx&所述; 3)6
           :((cell_idx< 6)7:8));
     }
     INT colIdx =(cell_idx%3)+((grid_idx%3)* 3);

     的System.out.println(网格=+ grid_idx +,细胞=+ cell_idx + - > sudoku2DGridArray [+ rowIdx +,+ colIdx +]);
  }
}
 

解决方案

什么

  rowIdx = 3 *(grid_idx / 3)+ cell_idx / 3
 

所有的刻度是整数除法

I'm working on a sudoku thing, and I'm having trouble reducing one part of it.

I'm designing a SudokuBoard object to have an underlying 2D byte array, and three different views of that array: row-units, column-units and grid units. A grid is the nine-by-nine blocks, indexed like this:

        GRID INDEX

        |         |
   0    |    1    |    2
        |         |
 -------------------------
        |         |
   3    |    4    |    5
        |         |
 -------------------------
        |         |
   6    |    7    |    8
        |         |

Where each grid has nine cells, indexed like this

CELL INDEX

   0 1 2
   3 4 5
   6 7 8

Here is the 2d array coordinates for the board:

                    2D-ARRAY COORDINATE

[0,0] [0,1] [0,2]  |  [0,3] [0,4] [0,5]  |  [0,6] [0,7] [0,8]
[1,0] [1,1] [1,2]  |  [1,3] [1,4] [1,5]  |  [1,6] [1,7] [1,8]
[2,0] [2,1] [2,2]  |  [2,3] [2,4] [2,5]  |  [2,6] [2,7] [2,8]
-------------------+---------------------+-------------------
[3,0] [3,1] [3,2]  |  [3,3] [3,4] [3,5]  |  [3,6] [3,7] [3,8]
[4,0] [4,1] [4,2]  |  [4,3] [4,4] [4,5]  |  [4,6] [4,7] [4,8]
[5,0] [5,1] [5,2]  |  [5,3] [5,4] [5,5]  |  [5,6] [5,7] [5,8]
-------------------+---------------------+-------------------
[6,0] [6,1] [6,2]  |  [6,3] [6,4] [6,5]  |  [6,6] [6,7] [6,8]
[7,0] [7,1] [7,2]  |  [7,3] [7,4] [7,5]  |  [7,6] [7,7] [7,8]
[8,0] [8,1] [8,2]  |  [8,3] [8,4] [8,5]  |  [8,6] [8,7] [8,8]

I've created a function to get the array-coordinate, given the grid and cell indexes. Determining the column index was a brain-buster, but I figured it out to be

int colIdx = (cell_idx % 3) + ((grid_idx % 3) * 3);

I also figured out do determine the row index, but I am unhappy with how complicated this is.

int rowIdx = -1;
if(grid_idx < 3)  {
   rowIdx = ((cell_idx < 3) ? 0
      :  ((cell_idx < 6) ? 1 : 2));
}  else if(grid_idx < 6)  {
   rowIdx = ((cell_idx < 3) ? 3
      :  ((cell_idx < 6) ? 4 : 5));
}  else  {
   rowIdx = ((cell_idx < 3) ? 6
      :  ((cell_idx < 6) ? 7 : 8));
}

If anyone has ideas on how to reduce this, ideally to a mathmatical formula, I'd appreciate it.

Here's a working application that demonstrates it. Thanks for any help.

  import  java.util.Arrays;
/**
   <P>{@code SudokuGridUnitCoordinates}</P>
 **/
public class SudokuGridUnitCoordinates  {
   public static final void main(String[] idxZeroGridIdx_0to8)  {
     int gridIdx = -1;
     try  {
        gridIdx = Integer.parseInt(idxZeroGridIdx_0to8[0]);
        if(gridIdx < 0  ||  gridIdx > 8)  {
           throw  new IllegalArgumentException();
        }
     }  catch(IllegalArgumentException | ArrayIndexOutOfBoundsException  x)  {
        throw  new IllegalArgumentException("The first element in idxZeroGridIdx_0to8 must be a digit zero through 8: " + Arrays.toString(idxZeroGridIdx_0to8));
     }

     printCellCoordinates(gridIdx, 0);
     printCellCoordinates(gridIdx, 1);
     printCellCoordinates(gridIdx, 2);
     printCellCoordinates(gridIdx, 3);
     printCellCoordinates(gridIdx, 4);
     printCellCoordinates(gridIdx, 5);
     printCellCoordinates(gridIdx, 6);
     printCellCoordinates(gridIdx, 7);
     printCellCoordinates(gridIdx, 8);
  }
  private static final void printCellCoordinates(int grid_idx, int cell_idx)  {
     int rowIdx = -1;
     if(grid_idx < 3)  {
        rowIdx = ((cell_idx < 3) ? 0
           :  ((cell_idx < 6) ? 1 : 2));
     }  else if(grid_idx < 6)  {
        rowIdx = ((cell_idx < 3) ? 3
           :  ((cell_idx < 6) ? 4 : 5));
     }  else  {
        rowIdx = ((cell_idx < 3) ? 6
           :  ((cell_idx < 6) ? 7 : 8));
     }
     int colIdx = (cell_idx % 3) + ((grid_idx % 3) * 3);

     System.out.println("grid=" + grid_idx + ", cell=" + cell_idx + " --> sudoku2DGridArray[" + rowIdx + ", " + colIdx + "]");
  }
}

解决方案

What about

rowIdx = 3*(grid_idx/3) + cell_idx/3

all division being integer divisions

这篇关于如何获得数独二维数组的索引,因为它与QUOT;分格&QUOT;和&QUOT;细胞内式分格&QUOT;索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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