Search code examples
javaarraysblock

I'm trying to write a code the takes two dimensional array "Sudoku" and arrange them into blocks


I'm trying to write a code the takes two dimensional array "Sudoku" (matrix.length * matrix.length) and I should check if the matrix is (n^2 * n^2) and a number (n) and I should arrange the rows into blocks (n * n). Any suggestions? if any body can do it if 4 "fors" i will be more than happy

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;
}

for exsample

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}}
*/

Solution

  • The formulas you're looking for are:

    int r = (i/sqrtN)*sqrtN+j/sqrtN;
    int c = (i%sqrtN)*sqrtN+j%sqrtN;
    returnedMatrix[i][j] = matrix[r][c];
    

    Full code with tests:

    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));
    }
    

    Output:

    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