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 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.

输入

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.

输出

For each test case draw an outline of the Sierpinski Triangle with a side‘s total length of 2ncharacters. 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.

样例输入

3
2
1
0

样例输出

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

解题思路

一开始总是以小三角形为单位,百思不得其解。看了一眼网上的思路之后恍然大悟要用数组做,递归的整体过程也写的很轻松,然后被各种换行空字符bug折磨,调了一个多小时orz。

AC代码

#include<iostream>
#include<cstring>
using namespace std;

char map[1038][2058];//x向上延展是行,y向右延展是列

void GetMap(int t, int x, int y)//x,y是每个三角形的起点
{
    if (t == 1)
    {
        map[x][y] = ‘/‘, map[x][y+1] = ‘_‘, map[x][y+2] = ‘_‘, map[x][y+3] = ‘\\‘;
        map[x + 1][y] = ‘ ‘, map[x + 1][y + 1] = ‘/‘, map[x + 1][y + 2] = ‘\\‘;
    }
    else
    {
        GetMap(t - 1, x, y);
        GetMap(t - 1, x, (1 << t) + y);
        GetMap(t - 1, (1 << (t - 1)) + x, (1 << (t - 1)) + y);
    }
}

void Draw(int t)
{
    int m = (1 << t) + 1;
    for (int i = 1<<t; i > 0; i--)
    {
        for (int j = 1; j <= m; j++)
        {
            if (map[i][j])
            {
                cout << map[i][j];
            }
            else cout << ‘ ‘;
        }
        cout << endl;
        m++;
    }
}

int main()
{
    int t;
    int num = 1;
    while (true)
    {
        cin >> t;
        if (t == 0)break;
        GetMap(t,1,1);
        if(num > 1) cout << endl;
        num++;
        Draw(t);
    }
    //system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/yun-an/p/10921075.html

时间: 2024-10-03 01:07:48

POJ 1941 The Sierpinski Fractal的相关文章

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

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

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 南京

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

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348

poj题库分类

初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,hea

POJ题目(转)

http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (

Poj 题目分类

初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,hea

POJ题目分类(转)

初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,hea