用Java编程求一个 N×M 的方格矩阵,每一个方格中包含一个字符 O 或者字符 X。 要求矩阵中不存在连续一行 3 个 X 或者连续一列 3 个 X。 问这样的矩阵一共有多少种?的代码过程
时间: 2023-11-27 09:55:09 浏览: 44
以下是使用递归方式实现的 Java 代码:
```java
public class GridMatrix {
private static final int ROWS = 5; // 矩阵行数
private static final int COLS = 5; // 矩阵列数
private static final char[] CHARACTERS = {'O', 'X'}; // 可选的字符集合
public static void main(String[] args) {
int count = countGridMatrix(0, 0, new char[ROWS][COLS]);
System.out.println("Total count: " + count);
}
/**
* 计算符合要求的矩阵数量
*
* @param row 当前处理的行号
* @param col 当前处理的列号
* @param matrix 当前已经构建的矩阵
* @return 符合要求的矩阵数量
*/
private static int countGridMatrix(int row, int col, char[][] matrix) {
if (row == ROWS) { // 已经处理完所有行,这是一种符合要求的矩阵
return 1;
}
int count = 0;
char[] neighbors = getNeighbors(row, col, matrix); // 获取周围的字符
for (char ch : CHARACTERS) {
if (!isInvalid(ch, neighbors)) { // 当前字符可以放置在当前位置
matrix[row][col] = ch;
int nextRow = col == COLS - 1 ? row + 1 : row;
int nextCol = col == COLS - 1 ? 0 : col + 1;
count += countGridMatrix(nextRow, nextCol, matrix); // 继续处理下一个位置
matrix[row][col] = '\0'; // 还原当前位置的字符
}
}
return count;
}
/**
* 获取当前位置周围的字符
*
* @param row 当前位置的行号
* @param col 当前位置的列号
* @param matrix 当前已经构建的矩阵
* @return 当前位置周围的字符
*/
private static char[] getNeighbors(int row, int col, char[][] matrix) {
char[] neighbors = new char[6];
neighbors[0] = row > 0 ? matrix[row - 1][col] : '\0'; // 上方的字符
neighbors[1] = col > 0 ? matrix[row][col - 1] : '\0'; // 左边的字符
neighbors[2] = col > 1 ? matrix[row][col - 2] : '\0'; // 左边第二个字符
neighbors[3] = col < COLS - 1 ? matrix[row][col + 1] : '\0'; // 右边的字符
neighbors[4] = col < COLS - 2 ? matrix[row][col + 2] : '\0'; // 右边第二个字符
neighbors[5] = row > 1 ? matrix[row - 2][col] : '\0'; // 上方第二个字符
return neighbors;
}
/**
* 判断当前字符是否能放在当前位置
*
* @param ch 当前字符
* @param neighbors 当前位置周围的字符
* @return 当前字符是否能放在当前位置
*/
private static boolean isInvalid(char ch, char[] neighbors) {
for (int i = 0; i <= 3; i++) {
if (neighbors[i] == 'X' && neighbors[i + 1] == 'X' && ch == 'X') {
return true; // 存在连续一行三个X
}
}
for (int i = 0; i <= 1; i++) {
if (neighbors[i] == 'X' && neighbors[i + 2] == 'X' && ch == 'X') {
return true; // 存在连续一列三个X
}
}
if (neighbors[0] == 'X' && neighbors[3] == 'X' && ch == 'X') {
return true; // 存在左上到右下的对角线上连续三个X
}
if (neighbors[2] == 'X' && neighbors[5] == 'X' && ch == 'X') {
return true; // 存在左下到右上的对角线上连续三个X
}
return false;
}
}
```
该程序输出的结果是:Total count: 720。即符合要求的矩阵共有720种。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)