POJ 2083 Fractal (打表乱搞)

Fractal
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 8339   Accepted: 3963

Description

A fractal is an object or quantity that displays self-similarity, in a somewhat technical sense, on all scales. The object need not exhibit exactly the same structure at all scales, but the same "type" of structures must appear on all scales.
A box fractal is defined as below :
  • A box fractal of degree 1 is simply
    X
  • A box fractal of degree 2 is
    X X
    X
    X X
  • If using B(n - 1) to represent the box fractal of degree n - 1, then a box fractal of degree n is defined recursively as following
    B(n - 1)        B(n - 1)
    
            B(n - 1)
    
    B(n - 1)        B(n - 1)

Your task is to draw a box fractal of degree n.

Input

The input consists of several test cases. Each line of the input contains a positive integer n which is no greater than 7. The last line of input is a negative integer −1 indicating the end of input.

Output

For each test case, output the box fractal using the 'X' notation. Please notice that 'X' is an uppercase letter. Print a line with only a single dash after each test case.

Sample Input

1
2
3
4
-1

Sample Output

X
-
X X
 X
X X
-
X X   X X
 X     X
X X   X X
   X X
    X
   X X
X X   X X
 X     X
X X   X X
-
X X   X X         X X   X X
 X     X           X     X
X X   X X         X X   X X
   X X               X X
    X                 X
   X X               X X
X X   X X         X X   X X
 X     X           X     X
X X   X X         X X   X X
         X X   X X
          X     X
         X X   X X
            X X
             X
            X X
         X X   X X
          X     X
         X X   X X
X X   X X         X X   X X
 X     X           X     X
X X   X X         X X   X X
   X X               X X
    X                 X
   X X               X X
X X   X X         X X   X X
 X     X           X     X
X X   X X         X X   X X
-


ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define fab(a) (a)>0?(a):(-a)
#define LL long long
#define MAXN 2200
#define mem(x) memset(x,0,sizeof(x))
#define INF 0xfffffff 
#define PI acos(-1.0)
using namespace std;
struct s
{
	char a[800][800];
	int size;
}map[10];
void fun(int n)
{
	int kk=map[n-1].size;
	int i,j;
	for(i=1;i<=kk;i++)
	{
		for(j=1;j<=kk;j++)
		map[n].a[i][j]=map[n-1].a[i][j];
		for(j=kk+1;j<=kk*2;j++)
		map[n].a[i][j]=' ';
		for(j=kk*2+1;j<=kk*3;j++)
		map[n].a[i][j]=map[n-1].a[i][j-2*kk];
	}
	for(i=kk+1;i<=kk*2;i++)
	{
		for(j=1;j<=kk;j++)
		map[n].a[i][j]=' ';
		for(j=kk+1;j<=kk*2;j++)
		map[n].a[i][j]=map[n-1].a[i-kk][j-kk];
		for(j=kk*2+1;j<=kk*3;j++)
		map[n].a[i][j]=' ';
	}
	for(i=kk*2+1;i<=kk*3;i++)
	{
		for(j=1;j<=kk;j++)
		map[n].a[i][j]=map[n-1].a[i-kk*2][j];
		for(j=kk+1;j<=kk*2;j++)
		map[n].a[i][j]=' ';
		for(j=kk*2+1;j<=kk*3;j++)
		map[n].a[i][j]=map[n-1].a[i-kk*2][j-kk*2];
	}
}
void db()
{
	int i;
	map[1].a[1][1]='X';map[1].size=1;
	map[2].a[1][1]='X';map[2].a[1][2]=' ';map[2].a[1][3]='X';
	map[2].a[2][1]=' ';map[2].a[2][2]='X';map[2].a[2][3]=' ';
	map[2].a[3][1]='X';map[2].a[3][2]=' ';map[2].a[3][3]='X';
	map[2].size=3;
	int k=9;
	for(i=3;i<=7;i++)
	{
		map[i].size=k;
		fun(i);
		k*=3;
	}
}
int main()
{
	int t,n,i,j;
	db();
	while(scanf("%d",&n)!=EOF)
	{
		if(n==-1)
		break;
		int len=map[n].size;
		for(i=1;i<=len;i++)
		{
			map[n].a[i][len+1]='\0';
			//for(j=1;j<=len;j++)
			printf("%s",map[n].a[i]+1);//一个一个输出超时了
			printf("\n");
		}
		printf("-\n");
	}
    return 0;
}


你可能感兴趣的:(POJ 2083 Fractal (打表乱搞))