簡體   English   中英

獲取數獨可能的解決方法

[英]Getting sudoku possible solutions Method

因此,這是此問題的后續問題:

從給定的row(x),column(y)中找到2D 9x9數組的3x3子數組

我正在編寫一種方法,該方法采用2D數組的行,列組合,在該特定位置掃描行,列和3x3子數組以查找可能的解決方案。 到目前為止,一切看起來都不錯。 第一個for循環掃描該行並將已使用的數字(1-9之間)存儲在一個hasInUse數組中。 第二個對列執行相同的操作。 第三個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;    
    }   
}

當我像這樣測試輸出時:

 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@659e0bfd即使調試顯示我已經包含了正確值的數組也是如此。 有人可以幫忙嗎?

您不能直接打印int數組。 你需要:

import java.util.Arrays;
...
System.out.println(Arrays.toString(sudoku.getPossibilities(3,4)));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM