java - Counting the number of times an element occurs in a given column for a 2D array? -


This is a method for a Sudoku game.

Can anyone explain their thinking processes to this? :( I am having difficulty finding this, I know for the first method, we have to use a loop or something. I know that for my method my walking is wrong ... Sorry: (

  private int countOccurrencesInCol (int col, int d) {for (int i = 0; i & lt; grid.length; i ++;) {for (int j = col; j = col) ; j ++;) {if (grid [i] [j] == d) {count ++;}}} Return count;}   

This method is a given column Returns the number of numbers in a 9 x 9 matrix.

  Private int countPossiblePlacementsInCol (int col , int d) {  

In this method, to determine the number of places is considered a given column where the numbers can be kept and the number of those places can be returned, Where a number can be placed on a given position for a given column. If the digit is already there, the method returns 0.

Here are some code to help you understand the essential approach:

  privat E int countOccurrencesInCol (int col, int d) {// counter variable occurrences to calculate int count = 0; // matrix is ​​2d array, the first index is row, column // is loop through each index of the second column, check for the number, int (int row = 0; line & lt; matrix.length; line ++) {// If any match is found ... if (matrix [row] [col] == D) // // Increasing the counter by a counter ++; }} // Returns the final count refund counters; }   

The next method is a little complex, because I do not need it but it is not clear. Should this method be returned only to the number of empty cells in the given column? Or should all the rules of Sudoku be kept in mind and should be examined whether the empty room is the valid tricks for that digit? If it is pre-fixed, the solution is simple:

  Private int countPossiblePlacementsInCol (int col, int d) {// I'm assuming an empty cell 0 indicates. In that case, // we can reuse the previous method to find the number of incidents of D, and the number of events of // 0, find out if the number is already on the line, then at 0 Come back: if (countOccurrencesInCol (Colonel, D)> 0) {return 0; } // Next, the number 0 comes back (number of blank cells): return number oncinconnect (call, 0); }   

However, if you count only the valid moves, then it becomes very complicated, the last line in the previous method will change to something else:

  Private int countPossiblePlacementsInCol (int col, int d) {// start the same as the previous method: if (countOccurrencesInCol (col, d)> 0) {return 0; } Int counter = 0; // This time, for each cell in the column, you have to check that this is a valid step: for (int row = 0; row & lt; matrix.length; row ++) {if (countOccurrencesInRow (line, D) == 0 & amp; amp; amp;; & amp; count cinquilites (line, call, d) == 0) {counter ++}}}   

I used this time Two methods, going to do something similar to countOccurrencesInRow and countOccurencesInSquare countOccurrencesInCol . line is basically like colonel , but it checks one line instead of one column. class is a bit tricky to assume that you know the rules of Sudoku, you should know that there is a square 3x3 section of 9x9 game board (which has 9x3x3 squares). According to counting the number of incoming points, using the square method line and call it will be found in which class the given cell is, and then through the rows and columns of that class Loop For this, you have to use two for loop, nested within each other, if you have trouble understanding how to use properly for loop, and loop through arrays, then I suggest that you Talk to your teacher / professor for additional support on the subject.

I hope that these explanations and examples help you best wishes.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -