链接:http://poj.org/problem?id=2083
Fractal
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 8119 | Accepted: 3890 |
Description
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 :
Your task is to draw a box fractal of degree n.
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 -
Source
大意——给你一个数n,显示对应的一个分形图形,B(n)由5个B(n-1)的图形组成,如下所示:
X
n=2时,图形是
X X
X
X X
n=3时,图形是
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
n最大为7。
B(n-1) B(n-1)
B(n-1)
B(n-1) B(n-1)
比如n=1时,图形是
X
n=2时,图形是
X X
X
X X
n=3时,图形是
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
n最大为7。
思路——因为n比较小,所以我们可以把n=7时的图形打印到2维数组中,然后读到n时,输出对应的图形即可。打印图形到2维数组中,使用递归是最自然的做法。因为这个分形图形本身定义就是递归的。因此我们只需要确定5个n-1规模问题的起始坐标和图形宽度就可以了。首先图形是一个正方形,n=1时,图形宽度是1;n=2时,图形宽度是3;显然,n时,图形宽度是3^(n-1)。接下来考虑起始坐标问题:假设w为n-1规模问题的图形宽度,n规模图形的起始坐标为(x,y),其被表示成5个n-1规模问题的组合。那么5个n-1规模图形的起始坐标依次是(x,y),(x,y+2w),(x+w,y+w),(x+2w,
y),(x+2w,y+2w)。解决了这两个问题,我们就可以递归打印图形了。
y),(x+2w,y+2w)。解决了这两个问题,我们就可以递归打印图形了。
复杂度分析——时间复杂度:O(1),空间复杂度:O(1)
附上AC代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <stack>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
typedef long long ll;
const double pi = acos(-1.0);
const double e = exp(1.0);
const double eps = 1e-8;
const short maxlen = 730;
short num[7] = {1, 3, 9, 27, 81, 243, 729}; // 图形宽度
char mat[maxlen][maxlen];
short n;
void print(short n); // 打印图形
void init(short n, short x, short y); // 预处理图形,递归实现
int main()
{
ios::sync_with_stdio(false);
memset(mat, ' ', sizeof(mat));
init(7, 0, 0);
while (~scanf("%hd", &n) && n!=-1)
print(n-1);
return 0;
}
void print(short n)
{
for (short i=0; i<num[n]; ++i)
{
for (short j=0; j<num[n]; ++j)
putchar(mat[i][j]);
putchar('\n');
}
printf("-\n");
}
void init(short n, short x, short y)
{
if (n == 1)
mat[x][y] = 'X';
else
{
short k = num[n-2];
init(n-1, x, y); // 五个部分图形起始坐标关系
init(n-1, x, y+2*k);
init(n-1, x+k, y+k);
init(n-1, x+2*k, y);
init(n-1, x+2*k, y+2*k);
}
}