分治画分形图 之 谢尔宾斯基三角形

谢尔宾斯基三角形(英语:Sierpinski triangle)是一种分形,由波兰数学家谢尔宾斯基在1915年提出。它是自相似集的例子。它的豪斯多夫维是log(3)/log(2) ≈ 1.585。

谢尔宾斯基三角形

今天我们来学学如何用代码生成任意第n图形

对于这个图形,显然我们知道它的每个子结构都是相同的,所以具有递归的性质

所以我们把握好递归边界,递归分治往下画就行了

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define tmax(a, b) if (b > a) a = b
#define tmin(a, b) if (b < a) a = b
char 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 pii;
typedef pair 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--------------------------
                               /\
                              /__\
                             /\  /\
                            /__\/__\
                           /\      /\
                          /__\    /__\
                         /\  /\  /\  /\
                        /__\/__\/__\/__\
                       /\              /\
                      /__\            /__\
                     /\  /\          /\  /\
                    /__\/__\        /__\/__\
                   /\      /\      /\      /\
                  /__\    /__\    /__\    /__\
                 /\  /\  /\  /\  /\  /\  /\  /\
                /__\/__\/__\/__\/__\/__\/__\/__\
               /\                              /\
              /__\                            /__\
             /\  /\                          /\  /\
            /__\/__\                        /__\/__\
           /\      /\                      /\      /\
          /__\    /__\                    /__\    /__\
         /\  /\  /\  /\                  /\  /\  /\  /\
        /__\/__\/__\/__\                /__\/__\/__\/__\
       /\              /\              /\              /\
      /__\            /__\            /__\            /__\
     /\  /\          /\  /\          /\  /\          /\  /\
    /__\/__\        /__\/__\        /__\/__\        /__\/__\
   /\      /\      /\      /\      /\      /\      /\      /\
  /__\    /__\    /__\    /__\    /__\    /__\    /__\    /__\
 /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\
/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\/__\


你可能感兴趣的:(分治)