您好我需要帮助从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;
}
这是我的validate类,它调用我已经实现的布尔表达式。我不确定在Validate中发送布尔值的参数。并考虑重新排列它,以便我发送3x3尺寸的块。
和c ++链接 Dividing a 9x9 2d array into 9 sub-grids (like in sudoku)? (C++)
答案 0 :(得分:2)
试试这段代码片段,它可以显示给定块内的不同索引,如使用int block = (((row / 3) * 3) + (column / 3));
import java.util.*;
public class TwoDArray
{
public static void main(String... args) throws Exception
{
Scanner scanner = new Scanner(System.in);
int[][] array = {
{0, 1, 2, 3, 4, 5, 6, 7, 8},
{9, 10, 11, 12, 13, 14, 15, 16, 17},
{18, 19, 20, 21, 22, 23, 24, 25, 26},
{27, 28, 29, 30, 31, 32, 33, 34, 35},
{36, 37, 38, 39, 40, 41, 42, 43, 44},
{45, 46, 47, 48, 49, 50, 51, 52, 53},
{54, 55, 56, 57, 58, 59, 60, 61, 62},
{63, 64, 65, 66, 67, 68, 69, 70, 71},
{72, 73, 74, 75, 76, 77, 78, 79, 80}
};
displayMatrix(array);
displayEachBlock(array);
}
private static void displayMatrix(int[][] array)
{
for (int i = 0; i < array.length; i++)
{
if (i == 3 || i == 6)
System.out.println("------------------------------------");
for (int j = 0; j < array[i].length; j++)
{
System.out.format("%-3s", array[i][j]);
if (j == 2 || j == 5 || j == 8)
System.out.print(" | ");
}
System.out.println();
}
System.out.println("------------------------------------");
}
private static void displayEachBlock(int[][] array)
{
for (int i = 0; i < array.length; i += 3)
{
for (int j = 0; j < array[i].length; j += 3)
{
/*
* Here we are finding which block we are standing at.
*/
int block = (((i / 3) * 3) + (j / 3));
System.out.println("Block : " + block);
int[][] newArray = new int[3][3];
int newRow = 0;
for (int k = i; k < (i + 3); k++)
{
int newColumn = 0;
for (int l = j; l < (j + 3); l++)
{
// This is where you are getting your array inside the given block.
newArray[newRow][newColumn] = array[k][l];
System.out.format("[%-1s][%-1s] : %-3s ", newRow, newColumn, newArray[newRow][newColumn++]);
}
newRow++;
System.out.println();
}
// Here you can send your newArray for VALIDATION, thingy.
// So that we can move on to the next Block for further processing.
}
}
}
}
以下是此代码段的输出:
0 1 2 | 3 4 5 | 6 7 8 |
9 10 11 | 12 13 14 | 15 16 17 |
18 19 20 | 21 22 23 | 24 25 26 |
------------------------------------
27 28 29 | 30 31 32 | 33 34 35 |
36 37 38 | 39 40 41 | 42 43 44 |
45 46 47 | 48 49 50 | 51 52 53 |
------------------------------------
54 55 56 | 57 58 59 | 60 61 62 |
63 64 65 | 66 67 68 | 69 70 71 |
72 73 74 | 75 76 77 | 78 79 80 |
------------------------------------
Block : 0
[0][0] : 0 [0][1] : 1 [0][2] : 2
[1][0] : 9 [1][1] : 10 [1][2] : 11
[2][0] : 18 [2][1] : 19 [2][2] : 20
Block : 1
[0][0] : 3 [0][1] : 4 [0][2] : 5
[1][0] : 12 [1][1] : 13 [1][2] : 14
[2][0] : 21 [2][1] : 22 [2][2] : 23
Block : 2
[0][0] : 6 [0][1] : 7 [0][2] : 8
[1][0] : 15 [1][1] : 16 [1][2] : 17
[2][0] : 24 [2][1] : 25 [2][2] : 26
Block : 3
[0][0] : 27 [0][1] : 28 [0][2] : 29
[1][0] : 36 [1][1] : 37 [1][2] : 38
[2][0] : 45 [2][1] : 46 [2][2] : 47
Block : 4
[0][0] : 30 [0][1] : 31 [0][2] : 32
[1][0] : 39 [1][1] : 40 [1][2] : 41
[2][0] : 48 [2][1] : 49 [2][2] : 50
Block : 5
[0][0] : 33 [0][1] : 34 [0][2] : 35
[1][0] : 42 [1][1] : 43 [1][2] : 44
[2][0] : 51 [2][1] : 52 [2][2] : 53
Block : 6
[0][0] : 54 [0][1] : 55 [0][2] : 56
[1][0] : 63 [1][1] : 64 [1][2] : 65
[2][0] : 72 [2][1] : 73 [2][2] : 74
Block : 7
[0][0] : 57 [0][1] : 58 [0][2] : 59
[1][0] : 66 [1][1] : 67 [1][2] : 68
[2][0] : 75 [2][1] : 76 [2][2] : 77
Block : 8
[0][0] : 60 [0][1] : 61 [0][2] : 62
[1][0] : 69 [1][1] : 70 [1][2] : 71
[2][0] : 78 [2][1] : 79 [2][2] : 80
答案 1 :(得分:1)
你有一个9x9的2D整数数组。您的目标是将其划分为9个3x3 2D整数数组。看一下check()
方法,该方法控制3x3数组的重复数字。
如果找到重复的数字,则返回false;
仔细检查并测试我的代码:
import java.util.ArrayList;
public class Main
{
public static void main(String[] args)
{
int[][] input = {{1,1,1,1,1,1,1,1,1},
{2,2,2,2,2,2,2,2,2},
{3,3,3,3,3,3,3,3,3},
{4,4,4,4,4,4,4,4,4},
{1,2,3,4,5,6,7,8,9},
{6,6,6,6,6,6,6,6,6},
{7,7,7,7,7,7,7,7,7},
{8,8,8,8,8,8,8,8,8},
{9,9,9,9,9,9,9,9,9}};
int[][][] output = get3DVersion(input);
for(int i=1; i<=output.length; i++)
System.out.println("Validity of subArray #"+i+" : " +check(output[i-1]));
}
public static boolean check(int[][] array)
{
ArrayList<Integer> soFar = new ArrayList<Integer>();
for(int j=0; j<3; j++)
for(int k=0; k<3; k++)
{
if(soFar.contains(array[j][k]))
return false;
else
soFar.add(array[j][k]);
}
return true;
}
public static int[][][] get3DVersion(int[][] input)
{
int[][][] output = new int[9][3][3];
for(int i=0; i<9; i++)
for(int j=0; j<3; j++)
for(int k=0; k<3; k++)
output[i][j][k] = input[i][j*3+k];
output[8][2][2] = input[8][8];
return output;
}
}