【算法练习】递归/图形化 百练poj1941:The Sierpinski Fractal

题目链接:http://bailian.openjudge.cn/practice/1941

参考链接:http://www.voidcn.com/article/p-srkptnpu-bnt.html

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

样例输出

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

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

 /\
/__\

题目:

我感觉主要还是要找规律 注意上图我画的地方

首先n和这个图形X Y的关系是怎么样的 是一个2倍数的关系

我们可以用移位来搞定

移位运算符的优先级移位运算的优先级小于加减运算的优先级

n=2的时候

X=4  =1<<n

Y=8 = 1<<n+1

递归每层 通过左上角的坐标,和第一层的时候输出那个基本的小三角形~

还是可以看上面的图 我们可以看到三个三角形的左上角顶点坐标如图~

//注意数组要开的够大

AC代码:

#include<iostream>
#include <string.h>
using namespace std;
int n;
const int maxn=(1<<11);  //一开始数组开到1<<10 报WA
char mp[maxn][maxn];
//代表左上角坐标 x y以及图形大小
void print(int x,int y,int sz){
    if(sz==1){
        mp[x][y+1]='/';
        mp[x][y+2]='\\';  //转义字符
        mp[x+1][y]='/';
        mp[x+1][y+3]='\\';
        mp[x+1][y+1]=mp[x+1][y+2]='_';
        return;
    }
    //上   左下↙    右下↘
    print(x,y+(1<<sz-1),sz-1);   //sz=2的时候就是 1<<1
    print(x+(1<<sz-1),y,sz-1);
    print(x+(1<<sz-1),y+(1<<sz),sz-1);
}

int main(){
    while(cin>>n){
        //初始化为空格
        if(n==0) break;
        for(int i=0;i<(1<<n);i++){
            for(int j=0;j<(2<<n);j++){
                mp[i][j]=' ';
            }
        }
        print(0,0,n);
        for(int i=0;i<(1<<n);i++){
            for(int j=0;j<(2<<n);j++){
               cout<<mp[i][j];
            }
            cout<<endl;
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40760678/article/details/100546963