先在二维数组中递归生成,然后再输出。
第一层递归:
对于每个区域分别进行第二层递归:
最终在最后一层递归将所有对应位置修改为‘X’。
#include <bits/stdc++.h>
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
const int N = 1e3 + 10;
char a[N][N];
int n;
int p[8];
void dfs(int u, int x, int y) {
if (u == 1) {
a[x][y] = 'X';
return;
}
int d = p[u - 2];
dfs(u - 1, x, y);
dfs(u - 1, x + 2 * d, y);
dfs(u - 1, x + d, y + d);
dfs(u - 1, x, y + 2 * d);
dfs(u - 1, x + 2 * d, y + 2 * d);
}
int main() {
p[0] = 1;
repi(i, 1, 7)p[i] = p[i - 1] * 3;
while (n = qr(), n != -1) {
int m = p[n - 1];
repi(i, 1, m) {
repi(j, 1, m)a[i][j] = ' ';
a[i][m + 1] = '\0';
}
dfs(n, 1, 1);
repi(i, 1, m)puts(a[i] + 1);
puts("-");
}
return 0;
}