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.

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

Ulm Local 2002

正解:模拟

解题报告:

  北大夏令营考过这道题,然而我没有做出来,当时真是傻了。

  递归构造字符,注意多余的要输出空格。

  看一下代码中的细节吧。

 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 }
时间: 2024-12-26 10:47:07

POJ1941 The Sierpinski Fractal的相关文章

POJ 1941 The Sierpinski Fractal

总时间限制: 1000ms 内存限制: 65536kB 描述 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

poj 1941 The Sierpinski Fractal 递归

//poj 1941 //sep9 #include <iostream> using namespace std; const int maxW=2048; const int maxH=1024; int pow2[32]; char g[maxH+10][maxW+10]; void print(int x,int y,int n) { if(n==1){ g[x][y+1]='/'; g[x][y+2]='\\'; g[x+1][y]='/'; g[x+1][y+1]='_'; g[x

POJ 1941 The Sierpinski Fractal ——模拟

只需要开一个数组,记录一下这个图形. 通过一番计算,发现最大的面积大约是2k*2k的 然后递归下去染三角形. 需要计算出左上角的坐标. 然后输出的时候需要记录一下每一行最远延伸的地方,防止行末空格过多. 然后需要用putchar #include <map> #include <cmath> #include <queue> #include <cstdio> #include <cstring> #include <iostream>

poj1941(递归)

The Sierpinski Fractal Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4773   Accepted: 1979 Description Consider a regular triangular area, divide it into four equal triangles of half height and remove the one in the middle. Apply the s

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

北大ACM题库习题分类与简介(转载)

在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------------------------------------------------------------------- acm.pku.edu.cn 1. 排序 1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376, 23

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116