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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...