Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从给定的行(x)、列(y)中,找到2D 9x9阵列的3x3子阵列_Java_Arrays_Multidimensional Array - Fatal编程技术网

Java 从给定的行(x)、列(y)中,找到2D 9x9阵列的3x3子阵列

Java 从给定的行(x)、列(y)中,找到2D 9x9阵列的3x3子阵列,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,所以我想把所有可能的条目都放到一个数独方块里。我有一个9x92D阵列,它被进一步划分为3x3子阵列。我想写一个方法,在其参数中采用行和列的组合,并返回在该特定位置可以创建的所有可能条目。 我的方法的前2个for循环获取指定的整行和整列中已经存在的所有非零值,并将它们存储在一个数组(alreadyInUse)中,这将在稍后阶段用于确定哪些数字尚未使用。第三个for循环应该使用行、列组合查找特定的子数组,并将其条目添加到alreadyInUse数组中 是否有任何方法可以使用2D数组的给定行、列来查找

所以我想把所有可能的条目都放到一个数独方块里。我有一个9x92D阵列,它被进一步划分为3x3子阵列。我想写一个方法,在其参数中采用行和列的组合,并返回在该特定位置可以创建的所有可能条目。 我的方法的前2个for循环获取指定的整行和整列中已经存在的所有非零值,并将它们存储在一个数组(alreadyInUse)中,这将在稍后阶段用于确定哪些数字尚未使用。第三个for循环应该使用行、列组合查找特定的子数组,并将其条目添加到alreadyInUse数组中

是否有任何方法可以使用2D数组的给定行、列来查找子数组的行、列

    // Method for calculating all possibilities at specific position
public int[] getPossibilities(int col, int row){
    int [] possibilities;
    int [] alreadyInUse = null;
    int currentIndex = 0;
    if(sudoku[row][col] != 0){
        return  new int[]{sudoku[col][row]};
    }
    else{
        alreadyInUse = new int[26];
        //Go into Row x and store all available numbers in an alreadyInUse
        for(int i=0; i<sudoku.length; i++){
            if(sudoku[row][i] !=0){
                alreadyInUse[currentIndex] = sudoku[row][i];
                currentIndex++;
            }
        }
        for(int j=0; j<sudoku.length; j++){
            if(sudoku[j][col] !=0){
                alreadyInUse[currentIndex] = sudoku[j][col];
                currentIndex++;
            }
        }
        for(int k=...???

    }
        return possibilities;
} 
//计算特定位置所有可能性的方法
公共int[]获取可能性(int列,int行){
int[]可能性;
int[]alreadyInUse=null;
int currentIndex=0;
如果(数独[行][列]!=0){
返回新的int[]{sudoku[col][row]};
}
否则{
alreadyInUse=新整数[26];
//进入第x行,将所有可用的数字存储在一个alreadyInUse中

对于(int i=0;i您可以使用模来过滤子数组。例如,一种方法是使用表达式
n-(n%3)
。例如,如果行是第8列(0索引数组中的最后一列),此表达式将返回6。它将也将为第6列返回6,但为第5列返回3

然后,一旦有了左上角的单元格,就可以使用嵌套循环遍历所有9个单元格,一次三个

以下是相关代码:

int x_left = (row - (row % 3));
int y_top = (col - (col % 3));
for(int i=0; i<3; i++) {
  for(int j=0; j<3; j++) {
     if(sudoku[i + x_left][j + y_top] != 0) {
       alreadyInUse[currentIndex] = sudoku[i + x_left][j + y_top];
       currentIndex++;
     }
  }
}
int x_left=(行-(行%3));
int y_top=(列-(列%3));

对于(int i=0;我猜是一个数独算法吗?哈哈。很抱歉这个蹩脚的问题。代码示例本身就有
Sudoku[][]
。有些天。。。