首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sudoku Checker java

Sudoku Checker java
EN

Stack Overflow用户
提问于 2014-11-18 00:55:17
回答 1查看 1.6K关注 0票数 0
代码语言:javascript
复制
public class Sudoku {
public static void main(String[] args) {
    // Row and column Latin but with invalid subsquares 
    String config1 = "1234567892345678913456789124567891235678912346" + "78912345789123456891234567912345678";
    String[][] puzzle1 = makeSudoku(config1);

    if (isValidSudoku(puzzle1)) {
        System.out.println("This puzzle is valid.");
    } else {
        System.out.println("This puzzle is invalid.");
    }


    System.out.println(getPrintableSudoku(puzzle1));

    System.out.println("--------------------------------------------------");

    // Row Latin but column not Latin and with invalid subsquares 
    String config2 = "12345678912345678912345678912345678912345678" + "9123456789123456789123456789123456789";
    String[][] puzzle2 = makeSudoku(config2);

    if (isValidSudoku(puzzle2)) {
        System.out.println("This puzzle is valid.");
    } else {
        System.out.println("This puzzle is invalid.");
    }

    System.out.println(getPrintableSudoku(puzzle2));
    System.out.println("--------------------------------------------------");

    // A valid sudoku 
    String config3 = "25813764914698532779324685147286319558149273663" + "9571482315728964824619573967354218";
    String[][] puzzle3 = makeSudoku(config3);
    if (isValidSudoku(puzzle3)) {
        System.out.println("This puzzle is valid.");
    } else {
        System.out.println("This puzzle is invalid.");
    }
    System.out.println(getPrintableSudoku(puzzle3));
    System.out.println("--------------------------------------------------");
}

public static String[][] makeSudoku(String s) {
    int SIZE = 9;
    int k = 0;
    String[][] x = new String[SIZE][SIZE];
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            x[i][j] = s.substring(k, k + 1);
            k++;
        }
    }
    return x;
}

public static String getPrintableSudoku(String[][] x) {
    int SIZE = 9;
    String temp = "";
    for (int i = 0; i < SIZE; i++) {
        if ((i == 3) || (i == 6)) {
            temp = temp + "=================\n";
        }
        for (int j = 0; j < SIZE; j++) {
            if ((j == 3) || (j == 6)) {
                temp = temp + " || ";
            }
            temp = temp + x[i][j];
        }
        temp = temp + "\n";
    }
    return temp;
}

//sudoku validation
public static boolean isValidSudoku(String[][] x) {
    return rowsAreLatin(x) && colsAreLatin(x) && goodSubsquares(x);
}

public static boolean rowsAreLatin(String[][] x) {
    // fill in your code here 
    boolean result = true; // Assume rows are latin 
    for (int i = 0; i < 9; i++) {
        result = result && rowIsLatin(x, i); // Make sure each row is latin 
    }
    return result;
}
public static boolean rowIsLatin(String[][] x, int i) {

    boolean[] found = new boolean[9];

    for (int j = 0; j < 9; j++) {
        found[Integer.parseInt(x[i][j])] = true;
    }

    for (int j = 0; j < 9; j++) {
        if (!found[j]) {
            return false;
        }
    }
    return true;
}

public static boolean colsAreLatin(String[][] x) {
    // fill in your code here 
    boolean result = true; // Assume cols are latin 
    for (int j = 0; j < 9; j++) {
        result = result && colIsLatin(x, j); // Make sure each row is latin 
    }
    return result;
}

public static boolean colIsLatin(String[][] x, int j) {
    // fill in your code here 
    boolean[] found = new boolean[9];
    for (int i = 0; i < 9; i++) {
        found[Integer.parseInt(x[i][j])] = true;
    }

    for (int i = 0; i < 9; i++) {
        if (!found[i]) {
            return false;
        }
    }
    return true;
}

public static boolean goodSubsquares(String[][] x) {

    return true;
}
public static boolean goodSubsquare(String[][] x, int i, int j) {

    boolean[] found = new boolean[9];
    // We have a 3 x 3 arrangement of subsquares 
    // Multiplying each subscript by 3 converts to the original array subscripts 
    for (int p = i * 3, rowEnd = p + 3; p < rowEnd; p++) {
        for (int q = j * 3, colEnd = q + 3; q < colEnd; q++) {

            found[Integer.parseInt(x[p][q])] = true;

        }
    }
    for (int p = 0; p < 9; p++) {
        if (!found[p]) {
            return false;
        }
    }
    return true;
}
}

这是我正在犯的错误。帮助!

代码语言:javascript
复制
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at Sudoku.rowIsLatin(Sudoku.java:104)
at Sudoku.rowsAreLatin(Sudoku.java:93)
at Sudoku.isValidSudoku(Sudoku.java:85)
at Sudoku.main(Sudoku.java:8)
Java Result: 1
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-18 01:02:10

问题在于这句话:

代码语言:javascript
复制
Integer.parseInt(x[i][j])

Sudoku数范围为1,9,但数组(长度为9)的索引范围为0,8。因此,当(i,j)元素为9时,索引为9,因此抛出IndexOutOfBoundsException。

你得把它改成

代码语言:javascript
复制
found[Integer.parseInt(x[i][j]) - 1] = true;

请注意,您还在列的相应方法中犯了相同的错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26984800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档