简体   繁体   中英

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

This is a method for a Sudoku game.

Could someone explain their thought processes for this? :( I'm having a lot of difficulty figuring this out. I know for the first method, we're supposed to use a for loop or something. I know my iteration for my method is wrong...Sorry :(

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

This method returns the number of times a digit occurs in a given column in a 9 X 9 matrix.

    private int countPossiblePlacementsInCol (int col, int d){

This method is supposed to determine the number of spots in a given column where a given digit can be placed, and returns the number of spots where a digit can be placed in a spot for a given column. If the digit already occurs, then the method returns 0.

Here is some code with explainations to help you understand the approach required:

private int countOccurrencesInCol(int col, int d) {
    // counter variable to count the number of occurrences
    int counter = 0;
    // matrix is the 2D array, the first index is the row, second is the column
    // loop through each index of the given column, checking for the digit, d
    for(int row = 0; row < matrix.length; row++) {
        // if a match is found...
        if(matrix[row][col] == d) {
            // increment the counter by one
            counter++;
        }
    }

    // return the final count
    return counter;
}

The next method is a bit trickier, because I am not clear on what is required. Is this method supposed to just return the number of empty cells in the given column? Or should it take into account all the rules of sudoku and check if those empty cells are valid moves for that digit? If it is the former, then the solution is simple:

private int countPossiblePlacementsInCol(int col, int d) {
    // I am assuming an empty cell is indicated by 0. In that case,
    // we can reuse the previous method to find the number of occurrences of d,
    // and the occurences of 0

    // first, find out if the digit already occurs in the row, return 0
    // if it does:
    if(countOccurrencesInCol(col, d) > 0) {
        return 0;
    }

    // next, return the number of times 0 occurs (the number of empty cells):
    return countOccurrencesInCol(col, 0);
}

However, if you must count only VALID moves, this gets a lot trickier. The last line in the previous method would turn into something more like this:

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 must check that it is a valid move:
    for(int row = 0; row < matrix.length; row++) {
        if(countOccurrencesInRow(row, d) == 0 &&
           countOccurencesInSquare(row, col, d) == 0) {
            counter++
        }
    }
}

The two methods I used this time, countOccurrencesInRow and countOccurencesInSquare are going to do something similar to countOccurrencesInCol . The Row one is basically the same as the Col one, but it checks a row instead of a column. The Square one is a bit trickier. Assuming you know the rules of sudoku, you should know that a square is a 3x3 section of the 9x9 game board (in which there are 9 3x3 sections). The Square method will use the row and col to figure out which square the given cell is in, and then loop through the rows and columns of just that square, counting occurrences of the given digit. For this, you will need to use two for loops, one nested inside the other. If you have trouble understanding how to properly use for loops, and loop through arrays, then I suggest you talk to your teacher/professor for additional help on the topic.

I hope these explanations and examples help. Good luck.

You do not need a second for loop.

Count of occurrence of number in a column can be found by

private int countOccurrencesInCol(int col, int d){
       for (int i = 0; i < grid.length; i++){
            if (grid[i][col] == d){
              count++;
            }
          }
       return count;
     }

Hope this helps!

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM