链接: https://vjudge.net/problem/POJ-1941
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 2 n 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
/\
/__\
/\ /\
/\/\
/\ /\
/\ /\
/\ /\ /\ /\
/\/\/\/\
/\
/__\
/\ /\
/\/\
/\
/__\
Hint
The Sierpinski-Triangle up to recursion depth 7
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <string>
#include <stack>
#include <algorithm>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
#define rep(i,a,n) for(int i=a;i<n;i++)
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
const int INF=0x3f3f3f3f;
const double PI=4*atan(1.0);
const int MOD=1e9+7;
const double eps=1e-8;
const int N=1e3+5;
char a[1025][2050];
void draw(int x,int y,int n) ///确定三角形上面的那个点
{
if(n==1)
{
a[x][y]=a[x+1][y-1]='/';
a[x][y+1]=a[x+1][y+2]='\\';
a[x+1][y]=a[x+1][y+1]='_';
return ;
}
int d=1<<(n-1);
draw(x,y,n-1);///当前的三角形
draw(x+d,y-d,n-1);///左下的三角形
draw(x+d,y+d,n-1);///右下的三角形
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
int h=(1<<n);
int w=(1<<(n+1));
for(int i=1; i<=h; i++)
for(int j=1; j<=w; j++)
a[i][j]=' ';
draw(1,1<<n,n);///画图
int k=(1<<n)+1;
for(int i=1; i<=h; i++,k++)
{
a[i][k+1]='\0';
puts(a[i]+1);
}
puts("");
}
return 0;
}
本文介绍如何使用ASCII字符绘制谢尔宾斯基三角形,这是一种典型的分形图案。通过递归算法,根据不同的递归深度,展示不同复杂度的三角形结构。
&spm=1001.2101.3001.5002&articleId=79172844&d=1&t=3&u=270ea79e58c04838aaeefd712daf7f3a)
2122

被折叠的 条评论
为什么被折叠?



