从 9x9 二维数组 java 创建 9 个子数组
您好,我需要帮助从 9x9 数组创建 9 个 3x3 维度的子数组。我看到 stackOverflow 已经提出了类似的问题,但不幸的是它是用 c++ 编写的。谁能指出我如何创建子数组的正确方向。
编辑:有一个类似的更改为有一个类似的
public static void Validate(final int[][] sudokuBoard)
{
int width = sudokuBoard[0].length;
int height = sudokuBoard.length;
for(int i = 0; i < width; i++)
if(!IsValidRow(sudokuBoard, i, width))
{
System.out.print("(Row)" + i + " Error Detected \n");
//Do something - The row has repetitions
}
for(int j = 0; j < height; j++)
if(!IsValidColumn(sudokuBoard, j, height))
{
System.out.print(" (Column)" + j + " Error Detected \n");
//Do something - The columns has repetitions
}
// for(int i=0; i<3; i++)
// if(!isBlock1Valid(sudokuBoard,width, height)){
// System.out.print("hi");
//}
}
static boolean isBlock1Valid(int[][] sudokuBoard, int referenceRow, int referenceColumn)
{
block1
boolean[] seen = new boolean[9];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if ( seen[sudokuBoard[referenceColumn+i][referenceRow+j]]) return false;
else ( seen[sudokuBoard[referenceColumn+i][referenceRow+j]]) = true;
}
}
return true;
}
这是我的验证类,它调用我已经实现的布尔表达式。我不确定在验证中发送布尔值的参数。并考虑重新排列它,以便我发送 3x3 尺寸的块。
和 C++ 链接 将 9x9 二维数组划分为9 个子网格(如数独)? (C++)
Hello I need help creating 9 sub arrays of 3x3 dimensions from a 9x9 array. I've seen that stackOverflow had a similar question already asked but unfortunately it was in c++. Can anyone point me in the right direction of how to create a a sub array.
Edit: had aa similar changed to had a similar
public static void Validate(final int[][] sudokuBoard)
{
int width = sudokuBoard[0].length;
int height = sudokuBoard.length;
for(int i = 0; i < width; i++)
if(!IsValidRow(sudokuBoard, i, width))
{
System.out.print("(Row)" + i + " Error Detected \n");
//Do something - The row has repetitions
}
for(int j = 0; j < height; j++)
if(!IsValidColumn(sudokuBoard, j, height))
{
System.out.print(" (Column)" + j + " Error Detected \n");
//Do something - The columns has repetitions
}
// for(int i=0; i<3; i++)
// if(!isBlock1Valid(sudokuBoard,width, height)){
// System.out.print("hi");
//}
}
static boolean isBlock1Valid(int[][] sudokuBoard, int referenceRow, int referenceColumn)
{
block1
boolean[] seen = new boolean[9];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if ( seen[sudokuBoard[referenceColumn+i][referenceRow+j]]) return false;
else ( seen[sudokuBoard[referenceColumn+i][referenceRow+j]]) = true;
}
}
return true;
}
this is my validate class that calls the boolean expression that i have implemented. I'm unsure the parameters to send the boolean in Validate. and thought about rearranging it so that i send Blocks of 3x3 dimensions instead.
and the c++ link
Dividing a 9x9 2d array into 9 sub-grids (like in sudoku)? (C++)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这个代码片段,它可以向您显示给定块内的不同索引,通过使用 int
block = (((row / 3) * 3) + (column / 3));
获得是此代码片段的输出:
Try this code snippet, it can show you different indices inside a given block, as obtained by using int
block = (((row / 3) * 3) + (column / 3));
And here is the output of this code snippet :
您有一个 9x9 二维整数数组。您的目标是将其分为 9 个 3x3 2D 整数数组。看一下
check()
方法,它控制 3x3 数组中的重复数字。如果发现重复数字,则返回 false;
检查并测试我的代码:
You have a 9x9 2D array of integers. Your aim is dividing it into 9 3x3 2D array of integers. Take a look at
check()
method which controls a 3x3 array for duplicate digits.If it finds duplicate digits, it returns false;
Scrutinize and test my code: