ZOJ1975 The Sierpinski Fractal

ZOJ1975 The Sierpinski Fractal

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Consider a regular triangular area, divide it into four equal triangles of half height and remove the one in the middle. Apply the same operation recursively to each of the three remaining triangles. If we repeated this procedure infinite times, we'd obtain something with an area of zero. The fractal that evolves this way is called the Sierpinski Triangle. Although its topological dimension is 2, its Hausdorff-Besicovitch dimension is log(3)/log(2)~1.58, a fractional value (that's why it is called a fractal). By the way, the Hausdorff-Besicovitch dimension of the Norwegian coast is approximately 1.52, its topological dimension being 1.

For this problem, you are to outline the Sierpinski Triangle up to a certain recursion depth, using just ASCII characters. Since the drawing resolution is thus fixed, you'll need to grow the picture appropriately. Draw the smallest triangle (that is not divided any further) with two slashes, to backslashes and two underscores like this:

 /\
/__\

To see how to draw larger triangles, take a look at the sample output.


Input

The input contains several testcases. Each is specified by an integer n. Input is terminated by n=0. Otherwise 1<=n<=10 indicates the recursion depth.


Output

For each test case draw an outline of the Sierpinski Triangle with a side's total length of 2n characters. Align your output to the left, that is, print the bottom leftmost slash into the first column. The output must not contain any trailing blanks. Print an empty line after each test case.


Sample Input

3
2
1
0


Sample Output

       /\
      /__\
     /\  /\
    /__\/__\
   /\      /\
  /__\    /__\
 /\  /\  /\  /\
/__\/__\/__\/__\

   /\
  /__\
 /\  /\
/__\/__\

 /\
/__\

********************************************************

题目大意:给定n,输出三角形。

解体思路:递归就好,不过想了段时间。还是那句话,分形,很好看啊。

//#pragma comment(linker,"STACK:65536000")
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#define N 15
#define M
#define E
#define inf 0x3f3f3f3f
#define eps 1e-8
#define linf (LL)1<<60
#define dinf 1e20
#define LL long long
#define clr(a,b) memset(a,b,sizeof(a))
#define D(a) ((a)*(a))
using namespace std;

int d;
char gra[5][5];
int r[N],c[N];

char gc(int x,int y,int d)
{
    if(d==1)return gra[x][y];
    if(x>=0&&x<r[d-1]&&((y>=0&&y<c[d-1]/2)||(y>=c[d-1]/2*3&&y<c[d])))return ' ';
    if(x>=0&&x<r[d-1])
        return gc(x,y-c[d-1]/2,d-1);
    if(y<c[d-1])
        return gc(x-r[d-1],y,d-1);
    return gc(x-r[d-1],y-c[d-1],d-1);
}

int main()
{
    //freopen("/home/fatedayt/in","r",stdin);
    gra[0][0]=' ';gra[0][1]='/';gra[0][2]='\\';gra[0][3]=' ';
    gra[1][0]='/';gra[1][1]='_';gra[1][2]='_';gra[1][3]='\\';
    r[1]=2;c[1]=4;
    for(int i=2;i<=10;i++)
    {
        r[i]=r[i-1]*2;
        c[i]=c[i-1]*2;
    }
	while(scanf("%d",&d),d)
	{
	    for(int i=0;i<r[d];i++)
	    {
	        int lim=-1;
	        for(int j=c[d]-1;j>=0;j--)
                if(gc(i,j,d)!=' ')
                {
                    lim=j;
                    break;
                }
            for(int j=0;j<=lim;j++)
                printf("%c",gc(i,j,d));
            puts("");
	    }
	    puts("");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Fatedayt/p/2432953.html