Java I';我正在写一个二维数组的代码;数独;然后把它们排成块
我想写一个二维数组“数独”的代码Java I';我正在写一个二维数组的代码;数独;然后把它们排成块,java,arrays,block,Java,Arrays,Block,我想写一个二维数组“数独”的代码 (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
(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;
}
如果定义类
网格
来表示数独网格,定义类单元格
来引用此网格的单元格,则可以使用单元格数组单元格[]
访问行、列和块:
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 N;
公共最终int n;
专用最终整数[][]矩阵;
公共类单元{
私人最终int行;
私人终审法院;
专用单元格(整数行,整数列){
this.row=行;
this.col=col;
}
公共int get(){
返回矩阵[行][col];
}
公共无效集(int值){
矩阵[行][列]=值;
}
}
公共网格(int[][]矩阵)引发异常{
N=矩阵长度;
n=(int)Math.sqrt(n);
如果(n*n!=n){
抛出新的IllegalArgumentException(
“矩阵大小必须是一个平方数”);
}
this.matrix=新整数[N][];
对于(int i=0;i=N){
抛出新IndexOutOfBoundsException(“无效行号:“+行”);
}
单元格[]结果=新单元格[N];
用于(整数列=0;列=N){
抛出新IndexOutOfBoundsException(“无效行号:+col”);
}
单元格[]结果=新单元格[N];
对于(int行=0;行=n){
抛出新的IndexOutOfBoundsException(“无效的块行:”+br);
}
如果(bc<0 | | bc>=n){
抛出新的IndexOutOfBoundsException(“无效块列:“+bc”);
}
int startRow=br*n;
int startCol=bc*n;
单元格[]结果=新单元格[N];
int k=0;
对于(int i=0;i
要列出所有块,请执行以下操作:
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("}");
}
}
int[]matrix1={
{11, 12, 13, 14},
{15, 16, 17, 18},
{19, 20, 21, 22},
{23, 24, 25, 26}};
网格网格=新网格(矩阵1);
对于(int i=0;i0){
系统输出打印(“,”);
}
System.out.print(块[k].get());
}
System.out.println(“}”);
}
}
使用
Cell
s而不是int
的优点是允许您更改单元格中的值。您要查找的公式有:
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));
}
你的问题是…?是的,我有一个建议:试着多想想,你称之为“块”是什么意思?
排列它们是什么意思?最后,这只是一个数组数组,与排列和排序无关blocks@SomeJavaGuy我只是放了一个样本不幸的是我不能用单元格来代替Int
,我也不能用网格
,我只能用for
,while
和基础为了按照他们想要的方式排列它们,我不想打印它们,我需要将它们保存在矩阵中,但您能解释一下这个公式的作用吗?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