Creating 9 subarrays from 9x9 2d java array

Hello, I need help creating 9 3x3 submatrices from a 9x9 array. I saw that stackOverflow was asked a similar question, but unfortunately it was in C ++. Can someone point me in the right direction how to create a helper array.

Edit: had a similar shift having 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 executed. I'm not sure if the parameters send a boolean to Validate. and thought about rearranging it so that it would send 3x3 blocks instead.

and C ++ link. Splitting a 9x9 2d array into 9 subnets (e.g. Sudoku)? (C ++)

+1
source share
2 answers

Try this piece of code, it can display different indices inside a given block, obtained using the int block = (((row / 3) * 3) + (column / 3)); function block = (((row / 3) * 3) + (column / 3));

 import java.util.*; public class TwoDArray { public static void main(String... args) throws Exception { Scanner scanner = new Scanner(System.in); int[][] array = { {0, 1, 2, 3, 4, 5, 6, 7, 8}, {9, 10, 11, 12, 13, 14, 15, 16, 17}, {18, 19, 20, 21, 22, 23, 24, 25, 26}, {27, 28, 29, 30, 31, 32, 33, 34, 35}, {36, 37, 38, 39, 40, 41, 42, 43, 44}, {45, 46, 47, 48, 49, 50, 51, 52, 53}, {54, 55, 56, 57, 58, 59, 60, 61, 62}, {63, 64, 65, 66, 67, 68, 69, 70, 71}, {72, 73, 74, 75, 76, 77, 78, 79, 80} }; displayMatrix(array); displayEachBlock(array); } private static void displayMatrix(int[][] array) { for (int i = 0; i < array.length; i++) { if (i == 3 || i == 6) System.out.println("------------------------------------"); for (int j = 0; j < array[i].length; j++) { System.out.format("%-3s", array[i][j]); if (j == 2 || j == 5 || j == 8) System.out.print(" | "); } System.out.println(); } System.out.println("------------------------------------"); } private static void displayEachBlock(int[][] array) { for (int i = 0; i < array.length; i += 3) { for (int j = 0; j < array[i].length; j += 3) { /* * Here we are finding which block we are standing at. */ int block = (((i / 3) * 3) + (j / 3)); System.out.println("Block : " + block); int[][] newArray = new int[3][3]; int newRow = 0; for (int k = i; k < (i + 3); k++) { int newColumn = 0; for (int l = j; l < (j + 3); l++) { // This is where you are getting your array inside the given block. newArray[newRow][newColumn] = array[k][l]; System.out.format("[%-1s][%-1s] : %-3s ", newRow, newColumn, newArray[newRow][newColumn++]); } newRow++; System.out.println(); } // Here you can send your newArray for VALIDATION, thingy. // So that we can move on to the next Block for further processing. } } } } 

And here is the result of this piece of code:

 0  1  2  | 3  4  5  | 6  7  8  | 9  10 11  | 12 13 14  | 15 16 17  | 18 19 20  | 21 22 23  | 24 25 26  | ------------------------------------ 27 28 29  | 30 31 32  | 33 34 35  | 36 37 38  | 39 40 41  | 42 43 44  | 45 46 47  | 48 49 50  | 51 52 53  | ------------------------------------ 54 55 56  | 57 58 59  | 60 61 62  | 63 64 65  | 66 67 68  | 69 70 71  | 72 73 74  | 75 76 77  | 78 79 80  | ------------------------------------ Block : 0 [0][0] : 0  [0][1] : 1  [0][2] : 2 [1][0] : 9  [1][1] : 10  [1][2] : 11 [2][0] : 18  [2][1] : 19  [2][2] : 20 Block : 1 [0][0] : 3  [0][1] : 4  [0][2] : 5 [1][0] : 12  [1][1] : 13  [1][2] : 14 [2][0] : 21  [2][1] : 22  [2][2] : 23 Block : 2 [0][0] : 6  [0][1] : 7  [0][2] : 8 [1][0] : 15  [1][1] : 16  [1][2] : 17 [2][0] : 24  [2][1] : 25  [2][2] : 26 Block : 3 [0][0] : 27  [0][1] : 28  [0][2] : 29 [1][0] : 36  [1][1] : 37  [1][2] : 38 [2][0] : 45  [2][1] : 46  [2][2] : 47 Block : 4 [0][0] : 30  [0][1] : 31  [0][2] : 32 [1][0] : 39  [1][1] : 40  [1][2] : 41 [2][0] : 48  [2][1] : 49  [2][2] : 50 Block : 5 [0][0] : 33  [0][1] : 34  [0][2] : 35 [1][0] : 42  [1][1] : 43  [1][2] : 44 [2][0] : 51  [2][1] : 52  [2][2] : 53 Block : 6 [0][0] : 54  [0][1] : 55  [0][2] : 56 [1][0] : 63  [1][1] : 64  [1][2] : 65 [2][0] : 72  [2][1] : 73  [2][2] : 74 Block : 7 [0][0] : 57  [0][1] : 58  [0][2] : 59 [1][0] : 66  [1][1] : 67  [1][2] : 68 [2][0] : 75  [2][1] : 76  [2][2] : 77 Block : 8 [0][0] : 60  [0][1] : 61  [0][2] : 62 [1][0] : 69  [1][1] : 70  [1][2] : 71 [2][0] : 78  [2][1] : 79  [2][2] : 80 
+2
source

You have an array of 9x9 2D integers. Your goal is to divide it into a 9 3x3 2D array of integers. Take a look at the check() method, which controls a 3x3 array for repeating digits. If it finds duplicate numbers, it returns false;

Hide and test my code:

 import java.util.ArrayList; 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}, {1,2,3,4,5,6,7,8,9}, {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=1; i<=output.length; i++) System.out.println("Validity of subArray #"+i+" : " +check(output[i-1])); } public static boolean check(int[][] array) { ArrayList<Integer> soFar = new ArrayList<Integer>(); for(int j=0; j<3; j++) for(int k=0; k<3; k++) { if(soFar.contains(array[j][k])) return false; else soFar.add(array[j][k]); } return true; } public static int[][][] get3DVersion(int[][] input) { int[][][] output = new int[9][3][3]; 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] = input[i][j*3+k]; output[8][2][2] = input[8][8]; return output; } } 
+1
source

Source: https://habr.com/ru/post/1439690/


All Articles