题目链接
http://poj.org/problem?id=2083
分析
开数组保存整个图形后打印,递归或递推皆可。
AC代码
#include <cstdio>
const int maxn = 10, maxl = 750;
int n, l[maxn];
bool f[maxn][maxl][maxl];
int main() {
l[1] = 1;
f[1][1][1] = 1;
for (int i = 2; i <= 7; ++i) {
int len = l[i - 1];
for (int j = 1; j <= len; ++j)
for (int k = 1; k <= len; ++k) {
f[i][j][k] = f[i - 1][j][k];
f[i][j][2 * len + k] = f[i - 1][j][k];
f[i][len + j][len + k] = f[i - 1][j][k];
f[i][2 * len + j][k] = f[i - 1][j][k];
f[i][2 * len + j][2 * len + k] = f[i - 1][j][k];
}
l[i] = 3 * l[i - 1];
}
while (scanf("%d", &n) == 1 && n != -1) {
int len = l[n];
for (int i = 1; i <= len; ++i) {
for (int j = 1; j <= len; ++j)
putchar(f[n][i][j] ? 'X' : ' ');
putchar('\n');
}
puts("-");
}
return 0;
}