数独检查器二维数组 Java
所以我的代码有点问题。它应该交叉检查行和列是否有相同的整数。
这是我到目前为止所拥有的..但是当我运行它时,它似乎只检查第一个整数。 (例如数独板的第一行读取。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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码比应有的更复杂。当您可以拆分为多个不同的函数时,为什么要将所有内容都放在一个函数中呢?
这样,您的代码就更容易维护/可读。上面的代码尚未经过测试,但应该是正确的。
如果您不清楚的话,我建议您逐步调试此代码,以真正了解发生了什么。
Your code is more complex than it should be. Why put everything in one single function when you could split in several different functions?
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.
给定数独的约束(一行 n 个单元格必须仅包含数字 1-n),您不需要顺序 n^2 搜索(每行或列),您可以通过保留一个位数组来执行顺序 n 指示你见过哪些数字。这是检查行的伪代码,对列执行相同的操作:
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:
我会将功能分解为更小的布尔检查。这样,您可以逐行、逐列、逐方地进行验证。例如,
请注意,行和列只需要传递一维数组。正方形是二维数组,因为您需要检查 3x3 区域。您还可以将这些方法视为静态方法,因为它们的功能独立于数独板实例。
编辑:关于行/列/方验证的建议是使用 HashSet。集合只能有 1 个特定值的元素,因此您可以添加元素并查找失败。例如:
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
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: