oslon Posted October 14 Share Posted October 14 How'd you do it? Let's hear feedback. Quote Link to comment Share on other sites More sharing options...
oslon Posted October 15 Author Share Posted October 15 https://ibb.co/64tsNgY How would you generate logic to check if each of the 9 small boxes contain elements from 1-9? Quote Link to comment Share on other sites More sharing options...
requinix Posted October 15 Share Posted October 15 You mean besides literally checking the rows, columns, and grids to see that they contain all of the numbers 1-9? Quote Link to comment Share on other sites More sharing options...
oslon Posted October 15 Author Share Posted October 15 I mean those 3x3 boxes. I want to check there (I can check for rows, columns) however I am lil bit confused on 3x3 boxes contains all digits or not. Quote Link to comment Share on other sites More sharing options...
requinix Posted October 15 Share Posted October 15 Are you able to check the rows and columns? It's the exact same thing but you're picking different squares... Exactly what part is it that you're having problems with? Do you have code for it so far? Quote Link to comment Share on other sites More sharing options...
oslon Posted October 16 Author Share Posted October 16 import java.util.*; public class Example { public static void main(String[] args) { int[][] solution = { {9, 6, 3, 1, 7, 4, 2, 5, 8}, {1, 7, 8, 3, 2, 5, 6, 4, 9}, {2, 5, 4, 6, 8, 9, 7, 3, 1}, {8, 2, 1, 4, 3, 7, 5, 9, 6}, {4, 9, 6, 8, 5, 2, 3, 1, 7}, {7, 3, 5, 9, 6, 1, 8, 2, 4}, {5, 8, 9, 7, 1, 3, 4, 6, 2}, {3, 1, 7, 2, 4, 6, 9, 8, 5}, {6, 4, 2, 5, 9, 8, 1, 7, 3} }; boolean ok = true; int[] count = new int[9]; for (int i = 0; i < solution.length; i++) { // -1 added for array index starting from 0, means 9 in above means count[8] for (int j = 0; j < solution[0].length; j++) { count[solution[i][j] - 1]++; } // ok or not checker for (int x = 0; x < count.length; x++) { System.out.println(count[x]); if (count[x] > 1) { ok = false; } } // reset the array count for next loop for (int k = 0; k < count.length; k++) { count[k] = 0; } } } } Â Quote Link to comment Share on other sites More sharing options...
oslon Posted October 16 Author Share Posted October 16 import java.util.*; public class Example { public static void main(String[] args) { int[][] solution = { {9, 6, 3, 1, 7, 4, 2, 5, 8}, {1, 7, 8, 3, 2, 5, 6, 4, 9}, {2, 5, 4, 6, 8, 9, 7, 3, 1}, {8, 2, 1, 4, 3, 7, 5, 9, 6}, {4, 9, 6, 8, 5, 2, 3, 1, 7}, {7, 3, 5, 9, 6, 1, 8, 2, 4}, {5, 8, 9, 7, 1, 3, 4, 6, 2}, {3, 1, 7, 2, 4, 6, 9, 8, 5}, {6, 4, 2, 5, 9, 8, 1, 7, 3} }; boolean ok = true; int[] count = new int[9]; for (int i = 0; i < solution.length; i++) { // -1 added for array index starting from 0, means 9 in above means count[8] for (int j = 0; j < solution[0].length; j++) { count[solution[i][j] - 1]++; } ok = checkIfOk(count); reset(count); } System.out.println(ok); } public static void reset(int[] arr) { for (int i = 0; i < arr.length; i++) { arr[i] = 0; } } public static boolean checkIfOk(int[] arr) { for (int i = 0; i < arr.length; i++) { if (arr[i] > 1) { return false; } } return true; } } Better modularized code Quote Link to comment Share on other sites More sharing options...
requinix Posted October 16 Share Posted October 16 So you have it checking all the rows, in that the outer loop counts rows (first index) and the inner loop looks at the cells in the row. You can use the exact same technique for checking the columns. (Hint: reverse your usage of the rows and columns.) That won't work for checking the grids, though. For the grids, you need to look at both rows and columns at once, but only a portion of them. So the "first" grid would be rows 0,1,2 and columns 0,1,2. "Second" would be rows 0,1,2 but columns 3,4,5. Then 0,1,2 and 6.7.8. Then repeat but with rows 3,4,5, and then again with 6,7,8. Give that a try and we'll see what you have. Quote Link to comment Share on other sites More sharing options...
oslon Posted October 16 Author Share Posted October 16 # For grids Now, I attempted to find a pattern. top left subgrid i=1-3 j=1-3 top middle subgrid i=1-3 j=4-6 top right subgrid i=1-3 j=7-9 middle left subgrid i=4-6 j=1-3 middle middle subgrid i=4-6 j=4-6 middle right subgrid i=4-6 j=7-9 bottom left subgrid i=7-9 j=1-3 bottom middle subgrid i=7-9 j=4-6 bottom right subgrid i=7-9 j=7-9 This is the pattern that I've caught. However, I am unable to exactly translate these stuffs to code. Â Checking row/col wise was easy, just interchanged j and i. Â import java.util.*; public class Example { public static void main(String[] args) { int[][] solution = { {9, 6, 3, 1, 7, 4, 2, 5, 8}, {1, 7, 8, 3, 2, 5, 6, 4, 9}, {2, 5, 4, 6, 8, 9, 7, 3, 1}, {8, 2, 1, 4, 3, 7, 5, 9, 6}, {4, 9, 6, 8, 5, 2, 3, 1, 7}, {7, 3, 5, 9, 6, 1, 8, 2, 4}, {5, 8, 9, 7, 1, 3, 4, 6, 2}, {3, 1, 7, 2, 4, 6, 9, 8, 5}, {6, 4, 2, 5, 9, 8, 1, 7, 3} }; boolean ok = true; int[] count = new int[9]; // row-wise loop for (int i = 0; i < solution.length; i++) { // -1 added for array index starting from 0, means 9 in above means count[8] for (int j = 0; j < solution[0].length; j++) { count[solution[i][j] - 1]++; } ok = checkIfOk(count); reset(count); } // column-wise loop for (int i = 0; i < solution.length; i++) { // -1 added for array index starting from 0, means 9 in above means count[8] for (int j = 0; j < solution[0].length; j++) { count[solution[j][i] - 1]++; } ok = checkIfOk(count); reset(count); } System.out.println(ok); } public static void reset(int[] arr) { for (int i = 0; i < arr.length; i++) { arr[i] = 0; } } public static boolean checkIfOk(int[] arr) { for (int i = 0; i < arr.length; i++) { if (arr[i] > 1) { return false; } } return true; } } Â Quote Link to comment Share on other sites More sharing options...
Barand Posted October 16 Share Posted October 16 If the individual cells are numbered 0 - 80 in 9 rows of 9 Then the index (0-8) of the large 3x3 square is given by function gridSquare($n) { return floor(floor($n/9) / 3) * 3 + floor($n % 9 / 3); } where $n i the nunber of the individual cell (0-80) Quote Link to comment Share on other sites More sharing options...
oslon Posted October 17 Author Share Posted October 17 @Barandsir. What do I do with that? I'm getting really puzzled by this puzzle. import java.util.*; public class Example { public static void main(String[] args) { int[][] solution = { {9, 6, 3, 1, 7, 4, 2, 5, 8}, {1, 7, 8, 3, 2, 5, 6, 4, 9}, {2, 5, 4, 6, 8, 9, 7, 3, 1}, {8, 2, 1, 4, 3, 7, 5, 9, 6}, {4, 9, 6, 8, 5, 2, 3, 1, 7}, {7, 3, 5, 9, 6, 1, 8, 2, 4}, {5, 8, 9, 7, 1, 3, 4, 6, 2}, {3, 1, 7, 2, 4, 6, 9, 8, 5}, {6, 4, 2, 5, 9, 8, 1, 7, 3} }; boolean ok = true; int[] count = new int[9]; // row-wise loop for (int i = 0; i < solution.length; i++) { // -1 added for array index starting from 0, means 9 in above means count[8] for (int j = 0; j < solution[0].length; j++) { count[solution[i][j] - 1]++; } ok = checkIfOk(count); System.out.println(ok); reset(count); } // column-wise loop for (int i = 0; i < solution.length; i++) { // -1 added for array index starting from 0, means 9 in above means count[8] for (int j = 0; j < solution[0].length; j++) { count[solution[j][i] - 1]++; } ok = checkIfOk(count); System.out.println(ok); reset(count); } // grid check 3x3 // 1st grid,2nd grid, 3rd grid for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { count[solution[i][j] - 1]++; } ok = checkIfOk(count); System.out.println(ok); reset(count); for (int j = 3; j < 6; j++) { count[solution[i][j] - 1]++; } ok = checkIfOk(count); System.out.println(ok); reset(count); for (int j = 6; j < 9; j++) { count[solution[i][j] - 1]++; } ok = checkIfOk(count); System.out.println(ok); reset(count); } // 4th,5th,6th grid // so on //7th,8th,9th grid // so on System.out.println(ok); } public static void reset(int[] arr) { for (int i = 0; i < arr.length; i++) { arr[i] = 0; } } public static boolean checkIfOk(int[] arr) { for (int i = 0; i < arr.length; i++) { if (arr[i] > 1) { return false; } } return true; } } I've came so far and there is no going back. I need to solve this. Quote Link to comment Share on other sites More sharing options...
oslon Posted October 17 Author Share Posted October 17 https://medium.com/strategio/sudoku-validator-algorithm-dc848cb45093 Found something similar to my approach. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 17 Share Posted October 17 17 hours ago, oslon said: What do I do with that? Well, one use would be to identify which cells are in each 9x9 square Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.