题目
A fractal is an object or quantity that displays self-similarity, in a somewhat technical sense, on all scales. The object need not exhibit exactly the same structure at all scales, but the same "type" of structures must appear on all scales.
A box fractal is defined as below :
A box fractal of degree 1 is simply
X
A box fractal of degree 2 is
X X
X
X X
If using B(n - 1) to represent the box fractal of degree n - 1, then a box fractal of degree n is defined recursively as following
B(n - 1) B(n - 1)
B(n - 1)
B(n - 1) B(n - 1)
Your task is to draw a box fractal of degree n.
Input
The input consists of several test cases. Each line of the input contains a positive integer n which is no greater than 7. The last line of input is a negative integer −1 indicating the end of input.
Output
For each test case, output the box fractal using the 'X' notation. Please notice that 'X' is an uppercase letter. Print a line with only a single dash after each test case.
Sample Input
1
2
3
4
-1
Sample Output
X
-
X X
X
X X
-
X X X X
X X
X X X X
X X
X
X X
X X X X
X X
X X X X
-
X X X X X X X X
X X X X
X X X X X X X X
X X X X
X X
X X X X
X X X X X X X X
X X X X
X X X X X X X X
X X X X
X X
X X X X
X X
X
X X
X X X X
X X
X X X X
X X X X X X X X
X X X X
X X X X X X X X
X X X X
X X
X X X X
X X X X X X X X
X X X X
X X X X X X X X
-
题解
递归
这题与poj3889 Fractal Streets相比要简单一些,这个在乎的是全局,那题在乎的是某个点。
既然是全局,而且还有重叠性,那么我们可以直接把n=7的情况处理出来,后面直接输出图形的左上角就好了。
不用担心要实时输出,可以用数组存下来,别太死脑筋。
这下,直接分5块递归就好啦。
注意输出时用puts是最快的。
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=10;
int bc[maxn];
char ma[800][800];
void dfs(int k,int x,int y)//绘制k等级的图
{
if(k==1)
{
ma[x][y]='X';
return ;
}
dfs(k-1,x,y);
dfs(k-1,x,y+2*bc[k-1]);
dfs(k-1,x+2*bc[k-1],y);
dfs(k-1,x+2*bc[k-1],y+2*bc[k-1]);
dfs(k-1,x+bc[k-1],y+bc[k-1]);
}
int main()
{
bc[1]=1;for(int i=2;i<=7;i++) bc[i]=bc[i-1]*3;
memset(ma,' ',sizeof(ma));
dfs(7,1,1);
int n;
while(scanf("%d",&n),n!=-1)
{
for(int i=1;i<=bc[n];i++)
{
ma[i][bc[n]+1]='\0';
puts(ma[i]+1);
ma[i][bc[n]+1]=' ';
}
printf("-\n");
}
return 0;
}