Monday, February 20, 2012

Creating 9 subarrays from a 9x9 2d array java


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++)

2 comments:

  1. I'm giving you the basics with integer array examples. Assume you have a 9x9 2D array of integers. Your aim is dividing it into 9 3x3 2D array of integers, meaning you want to have a 3D array of integers in the end. This is what I understood from your question.

    Firstly, convert your 9x9 2D array into 1D array having 81 elements, than construct a 3D array with these elements.

    Scrutinize and test my code:

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

    int[][][] output = get3DVersion(input);

    for(int i=0; i<output.length; i++)
    {
    for(int j=0; j<output[i].length; j++)
    {
    for(int k=0; k<output[j].length; k++)
    System.out.print(output[i][j][k] + " ");
    System.out.println();
    }
    System.out.println();
    }
    }

    public static int[][] get2DVersion(int[] input)
    {
    int[][] output = new int[9][9];

    for(int i=0; i<9; i++)
    for(int j=0; j<9; j++)
    output[i][j] = input[i*9+j];
    output[8][8] = input[80];

    return output;
    }

    public static int[][][] get3DVersion(int[] input)
    {
    int[][][] output = new int[9][3][3];
    int[][] newInput = get2DVersion(input);

    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] = newInput[i][j*3+k];

    output[8][2][2] = newInput[8][8];

    return output;
    }
    }

    ReplyDelete
  2. 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));

    import java.util.Scanner;

    public class TwoDArray
    {
    public static void main(String... args) throws Exception
    {
    Scanner scanner = new Scanner(System.in);
    int row = 9;
    int column = 9;
    int[][] array = new int[row][column];

    for (int i = 0; i < row; i += 3)
    {
    for (int j = 0; j < column; j += 3)
    {
    /*
    * Here we are finding which block we are standing at.
    */
    int block = (((i / 3) * 3) + (j / 3));
    System.out.println("Block : " + block);
    for (int k = i; k < (i + 3); k++)
    {
    for (int l = j; l < (j + 3); l++)
    {
    // This is where you are getting your array inside the given block.
    System.out.print("[ " + k + " ][ " + l + " ]\t");
    }
    System.out.println();
    }
    }
    }
    System.out.println("Width is : " + array[0].length);
    System.out.println("Height is : " + array.length);
    }
    }

    ReplyDelete