题解
2025-10-24 19:36:35
发布于:北京
1阅读
0回复
0点赞
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector<vector<char>> draw(int n) {
int size = pow(3, n - 1);
vector<vector<char>> grid(size, vector<char>(size, ' '));
if (n == 1) {
grid[0][0] = 'X';
return grid;
}
vector<vector<char>> smaller = draw(n - 1);
int small_size = smaller.size();
int step = small_size;
// 四个角
for (int i = 0; i < small_size; i++) {
for (int j = 0; j < small_size; j++) {
grid[i][j] = smaller[i][j]; // 左上
grid[i][j + 2 * step] = smaller[i][j]; // 右上
grid[i + step][j + step] = smaller[i][j]; // 中间
grid[i + 2 * step][j] = smaller[i][j]; // 左下
grid[i + 2 * step][j + 2 * step] = smaller[i][j]; // 右下
}
}
return grid;
}
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<vector<char>> result = draw(n);
int size = result.size();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cout << result[i][j];
}
cout << endl;
}
cout << "-" << endl;
}
return 0;
}
这里空空如也







有帮助,赞一个