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‘);
	}
}

  

时间: 2024-12-14 13:06:20

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

总时间限制: 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 3087 Shuffle&#39;m Up (模拟+map)

题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块牌归为s1,最顶的c块牌归为s2,依此循环下去. 现在输入s1和s2的初始状态 以及 预想的最终状态s12.问s1 s2经过多少次洗牌之后,最终能达到状态s12,若永远不可能相同,则输出"-1". 解题思路:照着模拟就好了,只是判断是否永远不能达到状态s12需要用map,定义map<

POJ 1027 The Same Game(模拟)

题目链接 题意 : 一个10×15的格子,有三种颜色的球,颜色相同且在同一片内的球叫做cluster(具体解释就是,两个球颜色相同且一个球可以通过上下左右到达另一个球,则这两个球属于同一个cluster,同时cluster含有至少两个球),每次选择cluster中包含同色球最多的进行消除,每次消除完之后,上边的要往下移填满空的地方,一列上的球移动之前与之后相对位置不变,如果有空列,右边的列往左移动,每一列相对位置不变 . 思路 : 模拟......不停的递归..... 1 ////POJ 102

POJ 3282 Ferry Loading IV(模拟,队列)

题意   汽车通过渡船过河  渡船开始在左边   输入按车辆来的顺序输入河两岸的车   渡船每次运输的汽车的总长度不能超过渡船自己本身的长度  先来的车先走   求轮船至少跨河多少次才能将所有的车辆都运完 简单模拟  建两个队列  分别装左边的车  和右边的车   算出两边各至少需要运输多少次就行了 #include<cstdio> #include<cstring> #include<queue> using namespace std; int main() { i

POJ 2424 Flo&#39;s Restaurant 模拟

Flo's Restaurant Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2923   Accepted: 916 Description Sick and tired of pushing paper in the dreary bleary-eyed world of finance, Flo ditched her desk job and built her own restaurant. In the s

poj 1008:Maya Calendar(模拟题,玛雅日历转换)

Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64795   Accepted: 19978 Description During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. From an old knotted message, profes

POJ 3087 Shuffle&#39;m Up(模拟)

Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several

poj 3087 Shuffle&#39;m Up (模拟过程)

Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several