java - 计算二维数组中某个元素在给定列中出现的次数?

标签 java arrays

这是数独游戏的一种方法。

有人可以解释一下他们的思维过程吗? :( 我很难弄清楚这一点。我知道对于第一种方法,我们应该使用 for 循环或其他东西。 我知道我的方法的迭代是错误的...抱歉:(

 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;
 }

此方法返回 9 X 9 矩阵中给定列中出现数字的次数。

    private int countPossiblePlacementsInCol (int col, int d){

此方法应该确定给定列中可以放置给定数字的点数,并返回给定列中可以放置数字的点数。如果该数字已经出现,则该方法返回 0。

最佳答案

以下是一些带有解释的代码,可帮助您理解所需的方法:

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;
}

下一个方法有点棘手,因为我不清楚需要什么。此方法是否应该只返回给定列中的空单元格数量?或者应该考虑数独的所有规则并检查这些空单元格是否是该数字的有效移动?如果是前者,那么解决办法很简单:

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);
}

但是,如果您必须只计算有效的移动,那么这会变得更加棘手。上一个方法中的最后一行将变成类似这样的内容:

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++
        }
    }
}

我这次使用的两个方法,countOccurrencesInRowcountOccurencesInSquare 将执行与 countOccurrencesInCol 类似的操作。 RowCol 基本相同,但它检查的是行而不是列。 Square 有点棘手。假设您了解数独规则,您应该知道正方形是 9x9 游戏板的 3x3 部分(其中有 9 个 3x3 部分)。 Square 方法将使用 row 和 col 来确定给定单元格位于哪个方格中,然后循环遍历该方格的行和列,计算给定数字的出现次数。为此,您需要使用两个 for 循环,一个嵌套在另一个循环内。如果您无法理解如何正确使用 for 循环和循环数组,那么我建议您向您的老师/教授咨询有关该主题的更多帮助。

我希望这些解释和示例有所帮助。祝你好运。

关于java - 计算二维数组中某个元素在给定列中出现的次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23197874/

相关文章:

java - 字段的默认值是否保证在线程中可见?

java - Azure ToolKit for Eclipse 安装问题

c# - 如何在控制台中将 5x10 二维数组与随机数据正确对齐?

java - 迭代快速排序方法

java - 如何将数组排序为列

java - Java小程序中keyListener的使用方法

java - List、List<?>、List<T>、List<E> 和 List<Object> 的区别

java - jni.h : No such file or directory

php - 按顺序用数字填充数组

javascript - 将数组作为(多)集合进行比较