Skip to main content

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


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

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex