从给定的行(X),柱(y)时,发现一个2D 9x9的阵列的3×3子阵列 [英] From given row(x),column(y), find 3x3 subarray of a 2D 9x9 array

查看:93
本文介绍了从给定的行(X),柱(y)时,发现一个2D 9x9的阵列的3×3子阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图让所有可以放入单数独方可能的条目。我有一个9x9的二维数组,其中被进一步分成3x3的子阵列。我想写,需要一个行和放大器的方法;列组合,它的参数和返回,可以在特定位置的所有可能的条目。
我的方法的第一2 for循环取整行&放大器在所有已经存在的非零值;被以阵列(alreadyInUse)指定的,并将其存储整列,这将在稍后阶段被用来找出尚未使用的号码。第三for循环应,用行,列组合,发现对特定子阵列,并添加其条目到alreadyInUse阵列。

有没有办法找到该行,该子数组列使用给定的行,二维数组的列?

  //计算方法在特定位置的所有可能性
公众诠释[] getPossibilities(INT山坳,诠释行){
    INT []可能性;
    INT [] alreadyInUse = NULL;
    INT CURRENTINDEX = 0;
    如果(数独[行] [山口]!= 0){
        返回新INT [] {数独[COL] [行]};
    }
    其他{
        alreadyInUse = INT新[26];
        //走进排x和存储在一个alreadyInUse所有可用号码
        的for(int i = 0; I< sudoku.length;我++){
            如果(数独[行] [I]!= 0){
                alreadyInUse [CURRENTINDEX] =数独[行] [I];
                CURRENTINDEX ++;
            }
        }
        对于(INT J = 0; J< sudoku.length; J ++){
            如果(数独[J] [COL]!= 0){
                alreadyInUse [CURRENTINDEX] =数独[J] [COL];
                CURRENTINDEX ++;
            }
        }
        对于(INT K = ...?    }
        返回的可能性;
}


解决方案

您可以使用模过滤掉子阵列。例如,一个办法做到这一点是使用前pression N - (N%3)。例如,如果行8列(在0数组索引的最后一列),这当然pression将返回6.这将同时作为列6回6,但它会返回3 5列。

然后,一旦你左上角的单元格,你可以通过使用一个嵌套的循环,三在同一时间全部9细胞循环。

下面是有关code:

  INT x_left =(行 - (行%3));
INT y_top =(COL - (COL%3));
的for(int i = 0;我3;;我++){
  为(中间体J = 0; J&下; 3; J ++){
     如果(数独[1 + x_left] [J + y_top]!= 0){
       alreadyInUse [CURRENTINDEX] =数独[1 + x_left] [J + y_top];
       CURRENTINDEX ++;
     }
  }
}

So i'm trying to get all the possible entries that can be put into a Sudoku single square. I have a 9x9 2D array, which is further divided into 3x3 subarrays. I want to write a method that takes a row & column combination in its parameters and returns all possible entries that can be made at that specific position. The first 2 for-loops of my method takes all the already existing non-zero values in the entire row & entire column that is specified and stores them in an array (alreadyInUse),this will at a later stage be used to figure out what numbers are not already used. The third for-loop should, using the row,column combination, find the specific subarray and add its entries to the alreadyInUse-array.

Is there any way to find the row,column of the subarray using given row,column of the 2D array?

    // 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;
} 

解决方案

You can use modulus to filter out the sub array. For example, one way to do it would be to use the expression n - (n % 3). For example, if row is column 8 (the last column in a 0 indexed array), this expression will return 6. It will also return 6 for column 6, but it will return 3 for column 5.

Then, once you have the top left cell, you can loop through all 9 cells using a nested loop, three at a time.

Here's relevant code:

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++;
     }
  }
}

这篇关于从给定的行(X),柱(y)时,发现一个2D 9x9的阵列的3×3子阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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