[英]Getting sudoku possible solutions Method
So this is a follow up question to this one: 因此,这是此问题的后续问题:
From given row(x),column(y), find 3x3 subarray of a 2D 9x9 array 从给定的row(x),column(y)中找到2D 9x9数组的3x3子数组
I'm writing a method that takes the row, column combination of a 2D array, scans the rows, columns and 3x3 subarrays for possible solutions at this specific position. 我正在编写一种方法,该方法采用2D数组的行,列组合,在该特定位置扫描行,列和3x3子数组以查找可能的解决方案。 So far, everything looks good. 到目前为止,一切看起来都不错。 The first for-loop scans the row and stores already used numbers (between 1-9) in an alreadyInUse array. 第一个for循环扫描该行并将已使用的数字(1-9之间)存储在一个hasInUse数组中。 The second does the same for the column. 第二个对列执行相同的操作。 The third for-loop determines the position of the subarray using the row,column combination, searches this 3x3 subarray and stores already found numbers in same alreadyInUse array. 第三个for循环使用行,列组合确定子数组的位置,搜索此3x3子数组并将已找到的数字存储在相同的existInUse数组中。
// Method for calculating all possibilities at specific position
public int[] getPossibilities(int row, int col){
int [] alreadyInUse;
int [] unsorted = new int[9];
int currentIndex = 0;
int size = 0;
boolean match;
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 alreadyInUse
for(int i=0; i<sudoku.length; i++){
if(sudoku[row][i] !=0){
alreadyInUse[currentIndex] = sudoku[row][i];
currentIndex++;
}
}
//Go into Column y and store all available numbers in alreadyInUse
for(int j=0; j<sudoku.length; j++){
if(sudoku[j][col] !=0){
alreadyInUse[currentIndex] = sudoku[j][col];
currentIndex++;
}
}
//Go into subarray and store all available numbers in alreadyInUse
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];
}
}
}
//compare 1-9 with the numbers stored in alreadyInUse array
for(int i=1; i<10; i++){
match = false;
//if a pair matches, break;
for(int j=0; j<alreadyInUse.length; j++){
if(alreadyInUse[j] == i ){
match = true;
break;
}
}
//If no pair matches, store the number (i) in an unsorted array
if(!match){
size++;
unsorted[i-1] = i;
}
}
//Re-use alreadyInUse array
alreadyInUse = new int[size];
size = 0;
//Remove the zeros and store the rest of the numbers in alreadyInUse array
for(int i=0; i<unsorted.length; i++){
if(unsorted[i] != 0){
alreadyInUse[size] = unsorted[i];
size++;
}
}
return alreadyInUse;
}
}
When i test the output like this: 当我像这样测试输出时:
public class SudokuTest {
public static void main(String[] args){
Sudoku sudoku;
String sud = "250030901010004000407000208005200000000098100040003000000360072070000003903000604";
sudoku = new Sudoku(sud);
System.out.println(sudoku.getPossibilities(3,4));
I get this as an output: [I@659e0bfd
even though the debug shows me that alreadyInUse array contains the correct values. 我将其作为输出得到: [I@659e0bfd
即使调试显示我已经包含了正确值的数组也是如此。 Can anybody help out here? 有人可以帮忙吗?
You can not directly print arrays of int. 您不能直接打印int数组。 You need to: 你需要:
import java.util.Arrays;
...
System.out.println(Arrays.toString(sudoku.getPossibilities(3,4)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.