poj Fractal 2083 (模拟&&dfs)

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
-

Source

大神的思路及分析,,。。。

/*n度的盒分形的规模为3^(n-1),即n度的盒分形图为一个长宽为3^(n-1)的正方形。
设置递归函数printBox(n,x,y)生成以坐标(x,y)为左上角的n度盒分形。

1)递归边界: 若n=1,则在(x,y)输出‘X’
2)若n>1,则计算n-1度的盒子的规模 m = 3^(n-2),分别在左上方, 右上方,中间,左下方和右下方画出5个n-1度的盒子。
对于左上方的n-1度的盒子,左上角的坐标为(x,y),递归printBox(n-1,x,y)生成;
对于右上方的n-1度的盒子,左上角的坐标为(x+2m,y),递归printBox(n-1,x+2m,y)生成;
对于中间的n-1度的盒子,左上角的坐标为(x+m,y+m),递归printBox(n-1,x+m,y+m)生成;
对于左下方的n-1度的盒子,左上角的坐标为(x,y+2m),递归printBox(n-1,x,y+2m)生成;
对于右上方n-1度的盒子,左上角的坐标为(x+2m,y+2m),递归printBox(n-1,x+2m,y+2m)生成;

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define N 1010
using namespace std;
char map[N][N];
void cop(int n,int x,int y)
{
	if(n==1)
		map[x][y]='X';
	else
	{
		int m=pow(3,n-2);
		cop(n-1,x,y);
		cop(n-1,x+2*m,y);
		cop(n-1,x,y+2*m);
		cop(n-1,x+m,y+m);
		cop(n-1,x+2*m,y+2*m);
	}
}
int main()
{
	int n,i,j;
	while(scanf("%d",&n)&&n!=-1)
	{
		int m=pow(3,n-1);
		for(i=0;i<m;i++)
		{
			for(j=0;j<m;j++)
			{
				map[i][j]=' ';
				map[i][m]='\0';
			}
		}
		cop(n,0,0);
		for(i=0;i<m;i++)
			printf("%s\n",map[i]);
		printf("-\n");
	}
	return 0;
}


 

 

你可能感兴趣的:(poj Fractal 2083 (模拟&&dfs))