POJ1941 The Sierpinski Fractal
Posted ljh2000
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ1941 The Sierpinski Fractal相关的知识,希望对你有一定的参考价值。
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.
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
/ /__ /\ / /__\/__ /\ / /__\ /__ /\ /\ /\ //__\/__\/__\/__
/ /__ /\ //__\/__
//__
Hint
The Sierpinski-Triangle up to recursion depth 7
Source
正解:模拟
解题报告:
北大夏令营考过这道题,然而我没有做出来,当时真是傻了。
递归构造字符,注意多余的要输出空格。
看一下代码中的细节吧。
1 //It is made by jump~ 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <ctime> 9 #include <vector> 10 #include <queue> 11 #include <map> 12 #include <set> 13 using namespace std; 14 typedef long long LL; 15 char ch[2048][2048]; 16 int n; 17 18 inline int getint() 19 { 20 int w=0,q=0; char c=getchar(); 21 while((c<‘0‘ || c>‘9‘) && c!=‘-‘) c=getchar(); if(c==‘-‘) q=1,c=getchar(); 22 while (c>=‘0‘ && c<=‘9‘) w=w*10+c-‘0‘, c=getchar(); return q ? -w : w; 23 } 24 25 inline void solve(int x,int y,int now){ 26 if(now==1) { 27 ch[x][y]=‘/‘; ch[x][y+1]=‘_‘; ch[x][y+2]=‘_‘; ch[x][y+3]=‘\\‘; 28 ch[x-1][y+1]=‘/‘; ch[x-1][y+2]=‘\\‘; 29 return ; 30 } 31 int du=(1<<now);//拆分成三个小三角形 32 solve(x,y,now-1); solve(x,y+du,now-1); solve(x-du/2,y+du/2,now-1); 33 } 34 35 inline void work(){ 36 while(1) { 37 n=getint(); if(n==0) break; 38 memset(ch,0,sizeof(ch)); int mi=(1<<n); 39 solve(mi,0,n); int last;//记录最后一个 40 for(int i=1;i<=mi;i++) { 41 for(int j=0;j<mi*2;j++) if(ch[i][j]) last=j; 42 for(int j=0;j<last;j++) if(!ch[i][j]) ch[i][j]=‘ ‘; 43 } 44 for(int i=1;i<=mi;i++) printf("%s\n",ch[i]); 45 printf("\n"); 46 } 47 } 48 49 int main() 50 { 51 work(); 52 return 0; 53 }
以上是关于POJ1941 The Sierpinski Fractal的主要内容,如果未能解决你的问题,请参考以下文章
POJ 1941 The Sierpinski Fractal ——模拟