我正在尝试编写一个采用二维数组“Sudoku”的代码
(matrix.length * matrix.length)
我应该检查矩阵是(n^2 * n^2)
还是数字(n)
,我应该将行排列成块(n * n)
。有什么建议?
如果有任何身体可以做到4“fors”我会非常高兴
public static int[][] blocks(int[][] matrix, int sqrtN) {
int [][] returnedMatrix = new int [matrix.length][matrix.length];
if (matrix.length != Math.pow(sqrtN, 2))
throw new RuntimeException("Matrix length is not" + Math.pow(sqrtN, 2 ));
for(int i=0 ; i <= matrix.length ;i=i+sqrtN) {
for(int j=sqrtN; j <= matrix.length;j=j+1) {
int temporarily =
}
}
return returnedMatrix;
}
例如
int[][] matrix1 = {{11,12,13,14},
{15,16,17,18},
{19,20,21,22},
{23,24,25,26}} ;
int[][] matBlocks1 = blocks (matrix1, 2) ;
/*
* matBlocks1 = {{11, 12, 15, 16},
* {13, 14, 17, 18},
* {19, 20, 23, 24},
* {21, 22, 25, 26}}
*/
答案 0 :(得分:0)
如果您定义一个类Grid
来表示一个Sudoku网格,一个类Cell
来引用该网格的一个单元格,您可以使用一组单元格来访问行,列和块{ {1}}:
Cell[]
要列出所有块,您可以:
public class Grid {
public final int N;
public final int n;
private final int[][] matrix;
public class Cell {
private final int row;
private final int col;
private Cell(int row, int col) {
this.row = row;
this.col = col;
}
public int get() {
return matrix[row][col];
}
public void set(int value) {
matrix[row][col] = value;
}
}
public Grid(int[][] matrix) throws Exception {
N = matrix.length;
n = (int)Math.sqrt(N);
if (n*n != N) {
throw new IllegalArgumentException(
"Matrix size must be a square number");
}
this.matrix = new int[N][];
for (int i = 0; i < N; ++i) {
int[] row = matrix[i];
if (row.length != N) {
throw new IllegalArgumentException("Matrix must be a square");
}
int[] newRow = new int[N];
for (int j = 0; j < N; ++j) {
newRow[j] = row[j];
}
this.matrix[i] = newRow;
}
}
public Cell cell(int row, int col) {
return new Cell(row, col);
}
public Cell[] row(int row) {
if (row < 0 || row >= N) {
throw new IndexOutOfBoundsException("Invalid row number: " + row);
}
Cell[] result = new Cell[N];
for (int col = 0; col < N; ++col) {
result[col] = cell(row, col);
}
return result;
}
public Cell[] colum(int col) {
if (col < 0 || col >= N) {
throw new IndexOutOfBoundsException("Invalid row number: " + col);
}
Cell[] result = new Cell[N];
for (int row = 0; row < N; ++row) {
result[row] = cell(row, col);
}
return result;
}
public Cell[] block(int br, int bc) {
if (br < 0 || br >= n) {
throw new IndexOutOfBoundsException("Invalid block row: " + br);
}
if (bc < 0 || bc >= n) {
throw new IndexOutOfBoundsException("Invalid block column: " + bc);
}
int startRow = br*n;
int startCol = bc*n;
Cell[] result = new Cell[N];
int k = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
result[k++] = cell(startRow+i, startCol+j);
}
}
return result;
}
}
使用 int[][] matrix1 = {
{11, 12, 13, 14},
{15, 16, 17, 18},
{19, 20, 21, 22},
{23, 24, 25, 26}};
Grid grid = new Grid(matrix1);
for (int i = 0; i < grid.n; ++i) {
for (int j = 0; j < grid.n; ++j) {
Cell[] block = grid.block(i, j);
System.out.print("{");
for (int k = 0; k < block.length; ++k) {
if (k > 0) {
System.out.print(", ");
}
System.out.print(block[k].get());
}
System.out.println("}");
}
}
代替Cell
的优势的好处是可以让您更改单元格中的值。
答案 1 :(得分:0)
您正在寻找的公式是:
int r = (i/sqrtN)*sqrtN+j/sqrtN;
int c = (i%sqrtN)*sqrtN+j%sqrtN;
returnedMatrix[i][j] = matrix[r][c];
包含测试的完整代码:
public static int[][] blocks(int[][] matrix, int sqrtN) {
int n = matrix.length;
if (n != Math.pow(sqrtN, 2))
throw new RuntimeException("Matrix length is not" + Math.pow(sqrtN, 2));
int[][] returnedMatrix = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int r = (i/sqrtN)*sqrtN+j/sqrtN;
int c = (i%sqrtN)*sqrtN+j%sqrtN;
returnedMatrix[i][j] = matrix[r][c];
}
}
return returnedMatrix;
}
public static void print(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
public static void main(String args[]) {
int[][] matrix4 = {
{1,1,2,2},
{1,1,2,2},
{3,3,4,4},
{3,3,4,4}
};
print(blocks(matrix4, 2));
int[][] matrix9 = {
{1,1,1,2,2,2,3,3,3},
{1,1,1,2,2,2,3,3,3},
{1,1,1,2,2,2,3,3,3},
{4,4,4,5,5,5,6,6,6},
{4,4,4,5,5,5,6,6,6},
{4,4,4,5,5,5,6,6,6},
{7,7,7,8,8,8,9,9,9},
{7,7,7,8,8,8,9,9,9},
{7,7,7,8,8,8,9,9,9}
};
print(blocks(matrix9, 3));
}
输出:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
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
5 5 5 5 5 5 5 5 5
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