POJ 1941 The Sierpinski Fractal ——模拟

只需要开一个数组,记录一下这个图形。

通过一番计算,发现最大的面积大约是2k*2k的

然后递归下去染三角形。

需要计算出左上角的坐标。

然后输出的时候需要记录一下每一行最远延伸的地方,防止行末空格过多。

然后需要用putchar

#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define ll long long
#define mp make_pair
#define maxn 2050

char s[maxn][maxn];

int l[11]={0,4,8,16,32,64,128,256,512,1024,2048},n,h[11]={0,2,4,8,16,32,64,128,256,512,1024};
int upper[maxn],high=0;

inline void solve(int x,int y,int siz)
{
	if (!siz) return;
	if (siz==1)
	{
		s[x][y]=' ';
		s[x][y+1]='/';
		s[x][y+2]='\\';
		s[x+1][y]='/';
		s[x+1][y+3]='\\';
		s[x+1][y+1]='_';
		s[x+1][y+2]='_';
		upper[x]=max(upper[x],y+2);
		upper[x+1]=max(upper[x],y+3);
		high=max(high,x+1);
		return ;
	}
	F(i,x,x+h[siz-1]-1) F(j,y,l[siz-1]/2+y-1) s[i][j]=' ';
	solve(x,y+l[siz-1]/2,siz-1);
	solve(x+h[siz-1],y,siz-1);
	solve(x+h[siz-1],y+l[siz-1],siz-1);
}

int main()
{
	while(scanf("%d",&n)!=EOF&&n)
	{
		F(i,0,h[n]) F(j,0,l[n]) s[i][j]=' ';
		high=0;memset(upper,0,sizeof upper);
		solve(0,0,n);
		F(i,0,high)
		{
			F(j,0,upper[i]) putchar(s[i][j]);
			putchar('\n');
		}
		putchar('\n');
	}
}

  

posted @ 2017-05-19 10:46  SfailSth  阅读(306)  评论(0编辑  收藏  举报