给定一个 1×1 的方格矩阵,方格为白色: 1.png 你需要对该矩阵进行 k 次扩张操作,并输出最终得到的矩阵。 扩张操作的具体规则如下。 首先,给定一个 n×n 的方格矩阵,其中的每个方格要么是白色,要么是黑色,称此矩阵为模板矩阵。 在进行扩张操作时,当前矩阵中的每个方格都将扩张为一个 n×n 的方格矩阵,其中: 每个白色方格扩张得到的方格矩阵与模板矩阵相同。 每个黑色方格扩张得到的方格矩阵只包含黑色方格。C++代码
时间: 2024-02-13 13:00:20 浏览: 176
```
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> expand(const vector<vector<int>>& matrix, int n) {
int m = matrix.size();
vector<vector<int>> res(m * n, vector<int>(m * n));
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == 0) { // 白色方格
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
res[i * n + x][j * n + y] = 0;
}
}
} else { // 黑色方格
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
res[i * n + x][j * n + y] = 1;
}
}
}
}
}
return res;
}
vector<vector<int>> k_expand(int k, const vector<vector<int>>& matrix, int n) {
if (k == 1) {
return matrix;
}
vector<vector<int>> tmp = expand(matrix, n);
return k_expand(k - 1, tmp, n);
}
int main() {
int k, n;
cin >> k >> n;
vector<vector<int>> matrix(1, vector<int>(1));
matrix[0][0] = 0; // 初始矩阵为白色
vector<vector<int>> res = k_expand(k, matrix, n);
int m = res.size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
cout << res[i][j] << " ";
}
cout << endl;
}
return 0;
}
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)