POJ 1941 The Sierpinski Fractal

来源:互联网 发布:小程序 app.js 编辑:程序博客网 时间:2024/06/07 03:18

Description

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

3210

Sample Output

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

Hint

 
The Sierpinski-Triangle up to recursion depth 7

Source

Ulm Local 2002
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

模拟~

其实就是递归,定义函数cal(int u,int x,int y)为在数组a的(x,y),开始的地方画一个n=u的三角形,当n==1时直接赋值即可。

注意:

1.输出的时候按字符输出会T,每次直接清空数组再按字符串输出反而很快;

2.赋值字符的时候,在定义时用双引号赋值整串,在函数中用单引号赋值单个字符,其中\要多加一个,用来做转义字符。


#include<cstdio>#include<cstring>#include<iostream>using namespace std;int n,tot;char a[2050][2050];void cal(int u,int x,int y){if(u==1){a[x][y]=' ';a[x][y+1]='/';a[x][y+2]='\\';a[x][y+3]=' ';a[x+1][y]='/';a[x+1][y+1]='_';a[x+1][y+2]='_';a[x+1][y+3]='\\';return;}for(int i=x;i<x+(1<<(u-1));i++)  for(int j=y;j<y+(1<<(u-1));j++) a[i][j]=' ';for(int i=x;i<x+(1<<(u-1));i++)  for(int j=y+(1<<(u+1))-1;y+(1<<(u+1))-j<=1<<(u-1);j--) a[i][j]=' ';cal(u-1,x+(1<<(u-1)),y);cal(u-1,x,y+(1<<(u-1)));cal(u-1,x+(1<<(u-1)),y+(1<<u));}int main(){while(scanf("%d",&n)==1 && n){memset(a,0,sizeof(a));tot=1<<n;cal(n,1,1);for(int i=1;i<=tot;i++){printf("%s",a[i]+1);puts("");}puts("");}return 0;}