繁体   English   中英

Sudoku Checker 2D数组Java

[英]Sudoku Checker 2d array Java

所以我的代码有点问题。.假设交叉检查行和列是否为相同的整数。

这是我到目前为止所拥有的..但是当我运行它时,它似乎只检查第一个整数。 (例如,数独板的第一行显示为1 2 2 2 2 2 2 2 2 2 2)它不会检测到明显的倍数2,但是如果我将输入更改为1 1 2 2 2 2 2 2 2 2,错误就会来临在这种情况下最多为1。 有很多建议可以调整我的循环以使其遍历各列?

public static void validate(final int[][] sudokuBoard) {
    int width = sudokuBoard[0].length;
    int depth = sudokuBoard.length;

    for (int i = 0; i < width; i++) {
            int j = i;
            int reference = sudokuBoard[i][j];

            while (true) {
                if ((j >= width) || (j >= depth)) {
                    break;
                } 
                else if (i == j){
                    // do nothing
                }
                else if (j < width) {
                    int current = sudokuBoard[i][j];

                    if (current == reference) {
                        System.out.print("Invalid entry found (width)" + "\n");
                        System.out.print(current + "\n");


                        // invalid entry found do something
                    }
                } else if (j < depth) {
                    // note reversed indexes
                    int current = sudokuBoard[j][i];

                    if (current == reference) {
                        System.out.print("Invalid entry found (depth)" + "\n");
                        System.out.print(current + "\n");

                        // invalid entry found do something
                    }
                }
                j++;
            }

您的代码比应该的要复杂。 当您可以拆分成几个不同的函数时,为什么将所有内容都放在一个函数中?

public static void Validate(final int[][] sudokuBoard)
{
    int width = sudokuBoard[0].length;
    int depth = sudokuBoard.length;

    for(int i = 0; i < width; i++)
        if(!IsValidRow(sudokuBoard, i, width))
        {
          //Do something - The row has repetitions
        }
    for(int j = 0; j < height; j++)
        if(!IsValidColumn(sudokuBoard, j, width))
        {
          //Do something - The columns has repetitions
        }
}

static bool IsValidRow(int[][] sudokuBoard, int referenceRow, int width)
{
    //Compare each value in the row to each other
    for(int i = 0; i < width; i++)
    {
        for(int j = i + 1; j < width; j++)
        {
            if(sudokuBoard[referenceRow][i] == sudokuBoard[referenceRow][j])
                return false
        }
    }
    return true;
}

static bool IsValidColumn(int[][] sudokuBoard, int referenceColumn, int height)
{
    //Compare each value in the column to each other
    for(int i = 0; i < height; i++)
    {
        for(int j = i + 1; j < height; j++)
        {
            if(sudokuBoard[i][referenceColumn] == sudokuBoard[j][referenceColumn])
                return false
        }
    }
    return true;
}

这样,您的代码将更易于维护/阅读。 上面的代码尚未经过测试,但应该正确。

我建议逐步调试该代码,以真正了解发生的事情(如果您不清楚)。

鉴于数独的限制(n个单元格的行必须仅包含数字1-n),您无需进行n ^ 2阶搜索(每行或每列),就可以通过保留一个表示以下内容的位数组来对n阶进行搜索您看到了哪些数字。 这是检查行的伪代码,对列执行相同的操作:

for i in 0 to depth-1 // rows
  boolean seen[] = new seen[width];
  for j in 0 to width-1 // columns
    if seen[board[i][j]-1] == true
      duplicate number
    else
      seen[board[i][j]-1] = true

我会将功能分解为较小的布尔检查。 这样,您可以逐行,逐列和逐平方验证。 例如

private boolean isValidRow(int[] row) {
    // Code here to check for valid row (ie, check for duplicate numbers)
}

private boolean isValidColumn(int[] column) {
    // Code here to check for valid column
}

private boolean isValidSquare(int[][] square) {
    // Code here to check for valid square
}

请注意,行和列仅需要传递一维数组。 正方形是二维数组,您需要检查3x3区域。 您也可以将这些方法视为static方法,因为它们的功能独立于Sudoku板实例。

编辑:关于行/列/正方形验证的建议是使用HashSet。 集合只能包含1个具有特定值的元素,因此您可以添加元素并查找失败。 例如:

HashSet<Integer> hs = new HashSet<Integer>();
for(int i = 0; i < 9; i++) {
    if(!hs.add(integerArray[i])) // HashSet.add returns 'false' if the add fails 
                                 // (ie, if the element exists)
        return false;
}
return true;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM