java - 数独解算器,让方框指向正确的方 block

标签 java arrays algorithm for-loop sudoku

我正在写一个数独解算器,现在我已经被困了几个小时,我就是无法让它工作。

解算器可以处理任何大小的棋盘,最多 64 个方 block ,方 block 可以是任何矩形。我已经创建了板并在二维数组中创建了所有的图 block ,所以这部分没问题。当我想将相同的瓷砖放入他们的盒子时,问题就出现了。

 0 -> 0 1 0 | 0 0 4 <- 1
      0 0 0 | 8 0 0 
      ------------- 
 2 -> 0 0 0 | 0 0 1 <- 3
      1 0 0 | 2 7 0
      ------------- 
 4 -> 0 0 0 | 9 0 0 <- 5
      0 2 0 | 0 0 0

所以像这样的棋盘,盒子的编号如上所示,从左到右。瓷砖的编号如下:

tile[row][column] 

例如:右上角的“4”是棋盘上的方 block [0][5]。

现在解决手头的问题。盒子有一个二维数组,大小和盒子一样,每个数组都应该指向棋盘上正确的方 block 。棋盘和盒子已经创建,我只需要盒子的数组指向棋盘上正确的图 block 。

例如:框号“3”应该有一个二维数组,其中六个值是:

[0][0] = 0; [0][1] = 0; [0][2] = 1;
[1][0] = 2; [1][2] = 7; [1][3] = 0;

我已经尝试了很多 for 循环嵌套,但我无法让它为每个给定场景选择正确的图 block 。给定的数独板可以是任何尺寸,盒子形状可以是任何矩形。

编辑:我写的代码,是基于挪威语的,有什么不明白的请问。

Edit2:问题出在Brett类的最后一个方法。

class SudokuSolver {
    public static void main(String[] args) throws Exception {
        Brett sudoku = lesFil(args[0]);



    }

    //Creates the sudoku board from a txt-file.
    public static Brett lesFil(String filnavn) throws Exception {
        Scanner fil = new Scanner(new File(filnavn));
        String linje = "";

        int antRad = Integer.parseInt(fil.nextLine());
        int antKol = Integer.parseInt(fil.nextLine());
        int antBoks = 0;
        //antall Bo(kser), Ra(der) og Ko(lonner)
        int antBoRaKo = antRad * antKol;
        Brett nyttBrett = new Brett(antBoRaKo, antRad, antKol);
    nyttBrett.bokser[antBoks] = new Boks(antBoRaKo);

        for(int i = 0; fil.hasNextLine(); i++) {
            if(i == antRad) {
        antBoks++;
                nyttBrett.bokser[antBoks] = new Boks(antBoRaKo);
            }
            linje = fil.nextLine();

            for(int j = 0; j < linje.length(); j++) {
                if(j == antKol) {
            antBoks++;
                    nyttBrett.bokser[antBoks] = new Boks(antBoRaKo);
                }

                Rute nyRute = new Rute(tegnTilVerdi(linje.charAt(j)));
                nyttBrett.ruter[i][j] = nyRute;
                nyttBrett.bokser[antBoks-1].ruter[i][j] = nyRute;
            }
        }

        return nyttBrett;
    }

    //Sign to value.
    //Gikk utifra at 0 skal representere en tom rute.
    public static int tegnTilVerdi(char tegn) {
        if (tegn == '.') {
            return 0;
        } else if ('1' <= tegn && tegn <= '9') {    // tegn er i [1, 9]
            return tegn - '0';
        } else if ('A' <= tegn && tegn <= 'Z') {    // tegn er i [A, Z]
            return tegn - 'A' + 10;
        } else if (tegn == '@') {                   // tegn er @
            return 36;
        } else if (tegn == '#') {                   // tegn er #
            return 37;
        } else if (tegn == '&') {                   // tegn er &
            return 38;
        } else if ('a' <= tegn && tegn <= 'z') {    // tegn er i [a, z]
            return tegn - 'a' + 39;
        } else {                                    // tegn er ugyldig
            return -1;
        }
    }
}

//Board-class where I am trying to match the boxes with the board.
class Brett {
Rute[][] ruter;
Boks[] bokser;
//antall bokser, antall rader og kolonner i hver boks.
int antBoks, antRad, antKol;

Brett(int antBoks, int antRad, int antKol) {
    ruter = new Rute[antBoks][antBoks];
    bokser = new Boks[antBoks];
    this.antBoks = antBoks;
    this.antRad = antRad;
    this.antKol = antKol;
}

//My last try of many, probably not my best try.
public void ruterIBoks2() {
    int cntRad = 0;
    for(int k = 0; k < antBoks; k++) {
        int cntKol = 1 * k;
        for(int i = 0; i < antRad; i++) {
            for(int j = 0; j < antKol; j++) {
                bokser[i][j] = ruter[cntRad][cntKol - 1];
                cntKol++;
            }
            cntRad++;
        }
    }
}

最佳答案

问题可能出在您的ruterIBoks2 方法中的cntKol 变量。

您将其设置为 k * 1(您可以将其设置为 k),一开始它等于 0,因为这是您的 for 条件。

问题在于您想要访问数组中的 cntKol - 1 元素。

bokser[i][j] = ruter[cntRad][cntKol - 1];

或者换句话说,第 -1 个元素,这可能是您遇到麻烦的原因。

关于java - 数独解算器,让方框指向正确的方 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36763034/

相关文章:

java - JAXB 将空字符串全局编码为空

arrays - Julia:如何更改存储在 StructArray 中的结构的字段值?

c++ - 生成两个随机数,使一个总是大于另一个

java.sql.SQLException : Parameter index of 9 is out of range (1, 8)

java - Java中的ValueErrors等?

php - 具有超过 3700 万种可能性的多个 foreach

javascript - 显示小时

python - 根据 cron spec 计算下一个预定时间

java - 与继承相关的家庭作业

java - 在给定数组中搜索多个数字