提交时间:2024-08-20 12:45:21

运行 ID: 168648

#include <bits/stdc++.h> using namespace std; char dt[500][500]; void dfs(int n, int x, int y) { if (n == 1) { dt[x][y] = 'X'; return; } int m = pow(3, n - 2); dfs(n - 1, x, y); dfs(n - 1, x + m * 2, y); dfs(n - 1, x + m, y + m); dfs(n - 1, x + m * 2, y + m * 2); dfs(n - 1, x, y + m * 2); } main() { int n; while (cin >> n && n != -1) { memset(dt, ' ', sizeof(dt)); dfs(n, 0, 0); for (int i = 0; i < pow(3, n - 1); i++) { for (int j = 0; j < pow(3, n - 1); j++) { cout << dt[i][j]; } cout << endl; } cout << "-" << endl; } }