POJ 2083 Fractal
来源:互联网 发布:货车软件 编辑:程序博客网 时间:2024/05/18 19:23
链接:http://poj.org/problem?id=2083
Fractal
Time Limit: 1000MS
Memory Limit: 30000KTotal Submissions: 8119
Accepted: 3890
Memory Limit: 30000KTotal 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
1234-1
Sample Output
X-X X XX X-X X X X X XX X X X X X X X XX X X X X XX X X X-
X X X X X X X X X X X XX X X X X X X X X X X X X X X X X XX X X X X X X X X X X XX X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X XX X X X X X X X X X X XX X X X X X X X X X X X X X X X X XX X X X X X X X X X X XX X X X X X X X-
Source
Shanghai 2004 Preliminary
大意——给你一个数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);}}
1 0
- Poj 2083 Fractal
- 【递归】poj 2083fractal
- POJ-2083-Fractal
- poj 2083 Fractal 递归
- POJ 2083 Fractal
- Fractal poj 2083
- POJ 2083 Fractal 递归
- POJ 2083 Fractal
- poj 2083 Fractal
- poj--2083--Fractal(dfs)
- POJ 2083 Fractal
- POJ 2083 Fractal
- POJ 2083 Fractal 笔记
- POJ 2083 Fractal 分治+递归
- POJ 2083 Fractal 递归画分形
- POJ-2083 Fractal-X星阵图
- POJ 2083 Fractal(递归)
- POJ 2083 Fractal(dfs)
- Mac 配置ruby环境之zsh vim
- Android View.OnTouchListener 的子类,AutoScrollHelper,ZoomButtonsController,ListViewAutoScrollHelper
- HDU 5202
- Hopfield's associative memory network
- Android ListView —— Adapter, BaseAdapter, RecycleBin
- POJ 2083 Fractal
- 基于SCN的查询(AS OF SCN)
- CURL抓取网页内容并用正则提取。
- 中断机制与内核定时器
- 将你的Vim 打造成轻巧强大的IDE
- 二级c++——基本操作题小结
- jieba中文分词源码分析(三)
- 使用接口求图形的周长与面积
- Spinner的用法