数独检查器二维数组 Java

发布于 2025-01-06 03:01:03 字数 1514 浏览 1 评论 0原文

所以我的代码有点问题。它应该交叉检查行和列是否有相同的整数。

这是我到目前为止所拥有的..但是当我运行它时,它似乎只检查第一个整数。 (例如数独板的第一行读取。1 2 2 2 2 2 2 2 2 2)它不会检测到明显的多个2,但如果我将输入更改为1 1 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++;
            }

So im having a bit of problem with my code.. It's suppose to cross check rows and columns for same integers.

this is what i have so far.. but when i run it, it only seems to check the first integer only. (for example the first line of the sudoku board reads. 1 2 2 2 2 2 2 2 2 2) it wont detect the obvious multiple 2's but if i change the input to 1 1 2 2 2 2 2 2 2 the error will come up of multiple 1's in this case. the multiple any suggestions to tweak my loops to make it go through the columns?

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++;
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

会发光的星星闪亮亮i 2025-01-13 03:01:03

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

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;
}

这样,您的代码就更容易维护/可读。上面的代码尚未经过测试,但应该是正确的。

如果您不清楚的话,我建议您逐步调试此代码,以真正了解发生了什么。

Your code is more complex than it should be. Why put everything in one single function when you could split in several different functions?

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;
}

That way, your code is much more easily maintainable/readable. This code above hasn't been tested, but it should be about right.

I suggest debugging this code step by step to really understand what's going on, if that's not clear for you.

只为守护你 2025-01-13 03:01:03

给定数独的约束(一行 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

Given the constraints of sudoku (a row of n cells must contain the numbers 1-n only) you don't need an order n^2 search (per row or column), you can do it order n by keeping a bit array indicating which numbers you've seen. Here's the pseudo-code for checking rows, do the same for columns:

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
晨与橙与城 2025-01-13 03:01:03

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

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 区域。您还可以将这些方法视为静态方法,因为它们的功能独立于数独板实例。

编辑:关于行/列/方验证的建议是使用 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;

I would break the functionality into smaller boolean checks. This way, you can validate row by row, column by column, and square by square. For instance

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
}

Note that rows and columns only need to be passed a 1 dimensional array. Squares are a 2 dimensional array as you need to check a 3x3 area. You can also treat these methods as static as their functionality is independent of the Sudoku board instance.

Edit: A suggestion on row/column/square validation is to use a HashSet. Sets can only have 1 element of a certain value, so you can add elements and look for a failure. For example:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文