分治画分形图 之 谢尔宾斯基三角形
来源:互联网 发布:淘宝如何设置手机客服 编辑:程序博客网 时间:2024/06/07 08:49
谢尔宾斯基三角形(英语:Sierpinski triangle)是一种分形,由波兰数学家谢尔宾斯基在1915年提出。它是自相似集的例子。它的豪斯多夫维是log(3)/log(2) ≈ 1.585。
今天我们来学学如何用代码生成任意第n图形
对于这个图形,显然我们知道它的每个子结构都是相同的,所以具有递归的性质
所以我们把握好递归边界,递归分治往下画就行了
代码:
#include <iostream>#include <vector>#include <string>#include <algorithm>#include <cstdio>#include <cstring>#include <ctime>#include <cstdlib>#include <set>#include <map>#include <cctype>#include <list>#include <cmath>#include <bitset>#include <queue>#include <stack>#include <sstream>#include <functional>#include <cassert>using namespace std;#define tmax(a, b) if (b > a) a = b#define tmin(a, b) if (b < a) a = bchar inc() { char _[10]; scanf("%s", _); return _[0]; }int ini() { int _; scanf("%d", &_); return _; }long long inll() { long long _; scanf("%I64d", &_); return _; }double ind() { double _; scanf("%lf", &_); return _; }string ins() { string _; cin >> _; return _; }int inl(char _[]) { if (!fgets(_, (int)1e8, stdin)) return -1; int i = strlen(_); if (_[i - 1] == '\n') _[--i] = 0; return i; }typedef pair<int, int> pii;typedef pair<char, char> pcc;typedef long long LL;const int inf = 0x3f3f3f3f;const LL lnf = 0x3f3f3f3f3f3f3f3f;const double pi = 3.14159265358979323846;const double eps = 1e-8;const int mod = 100007;const int maxn = 1e3+ 10;char mp[maxn][maxn];void Draw(int n, int x, int y) { if (n == 1) { mp[x][y + 1] = mp[x][y + 2] = '_'; mp[x][y] = mp[x - 1][y + 1] = '/'; mp[x - 1][y + 2] = mp[x][y + 3] = '\\'; return; } int w = 1 << n; int h = w >> 1; Draw(n - 1, x, y); Draw(n - 1, x, y + w); Draw(n - 1, x - h, y + w / 2);}int main() { int CAS = 0; //std::ios::sync_with_stdio(0); //std::cin.tie(0);#ifdef NIGHT_13 freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); int time_night_13 = clock();#endif // NIGHT_13 int n = ini(); Draw(n, (1 << n) - 1 , 0); for (int i = 0, st = 1 << n; i < 1 << n; ++i, ++st) { for (int j = 0; j <= st; ++j) { printf("%c", mp[i][j] ? mp[i][j] : ' '); } puts(""); }#ifdef NIGHT_13 fprintf(stderr, "\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); fprintf(stderr, "\t Time: %dms", (int)clock() - time_night_13); fprintf(stderr, "\n...........................................\n\n");#endif // NIGHT_13 return 0;}
运行效果:
-------------------n = 1-------------------------- /\/__\-------------------n = 2-------------------------- /\ /__\ /\ /\/__\/__\-------------------n = 3-------------------------- /\ /__\ /\ /\ /__\/__\ /\ /\ /__\ /__\ /\ /\ /\ /\/__\/__\/__\/__\-------------------n = 4-------------------------- /\ /__\ /\ /\ /__\/__\ /\ /\ /__\ /__\ /\ /\ /\ /\ /__\/__\/__\/__\ /\ /\ /__\ /__\ /\ /\ /\ /\ /__\/__\ /__\/__\ /\ /\ /\ /\ /__\ /__\ /__\ /__\ /\ /\ /\ /\ /\ /\ /\ /\/__\/__\/__\/__\/__\/__\/__\/__\-------------------n = 5-------------------------- /\ /__\ /\ /\ /__\/__\ /\ /\ /__\ /__\ /\ /\ /\ /\ /__\/__\/__\/__\ /\ /\ /__\ /__\ /\ /\ /\ /\ /__\/__\ /__\/__\ /\ /\ /\ /\ /__\ /__\ /__\ /__\ /\ /\ /\ /\ /\ /\ /\ /\ /__\/__\/__\/__\/__\/__\/__\/__\ /\ /\ /__\ /__\ /\ /\ /\ /\ /__\/__\ /__\/__\ /\ /\ /\ /\ /__\ /__\ /__\ /__\ /\ /\ /\ /\ /\ /\ /\ /\ /__\/__\/__\/__\ /__\/__\/__\/__\ /\ /\ /\ /\ /__\ /__\ /__\ /__\ /\ /\ /\ /\ /\ /\ /\ /\ /__\/__\ /__\/__\ /__\/__\ /__\/__\ /\ /\ /\ /\ /\ /\ /\ /\ /__\ /__\ /__\ /__\ /__\ /__\ /__\ /__\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\
0 0
- 分治画分形图 之 谢尔宾斯基三角形
- BZOJ 2458 BeiJing 2011 最小三角形 分治
- 【BZOJ2458】【BeiJing2011】最小三角形 计算几何+分治
- 【分治】【bzoj 2458】: [BeiJing2011]最小三角形
- bzoj 2458: [BeiJing2011]最小三角形 分治
- POJ1741树的分治之点分治
- 算法之分治算法
- 分治法之我见
- 分治算法之汉诺塔
- 算法之分治法
- 算法之分治思想
- 算法设计之分治
- 算法之分治法
- leetcode之分治总结
- 算法系列之分治
- hdu1007之分治
- BZOJ 2458 BeiJing2011 最小三角形 计算几何+分治
- 函数模版之三角形
- NTFS文件系统结构--从零开始追踪一个文件的位置
- Unity3D保存一张1080p(200W像素)以上的大图片 并打印
- org.hibernate.exception.GenericJDBCException: could not execute query项目登录验证报错
- poj 2406 Power Strings(倍增TLE版)
- POJ1852蚂蚁问题.cpp
- 分治画分形图 之 谢尔宾斯基三角形
- Microsoft visio 2010 Premium 的激活
- android6.0以上版本蓝牙4.0开发
- JAVA RGB转CMYK 源码(支持格式转换)
- Mysql中的sql server 与mysql 与orcle与acess与sql与mssql大体了解
- 【Linux】僵尸进程和孤儿进程
- LightOJ 1282 Leading and Trailing
- Java基础之Socket编程
- php中$this->是什么意思