POJ - 2083 Fractal 图像递归

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 

  • A box fractal of degree 2 is 
    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
-

这个题目还是先观察,发现第n个图形是由5个第n-1个图像组成的 中心对称,那么我们先可以找到第n个图像的中心坐标,然后分别求出5个n-1个图像的中心坐标,递归,当n等于1时,那个中心坐标赋予x,return;这样就把图像搞好了,至于中心坐标的改变规律可以看代码



#include<iostream>  
#include<cstdio>  
#include<algorithm>
#include<cmath>
#include<queue> 
#include<cstring>
#include<bitset>
#include<map>
using namespace std;  
#define ll long long  
typedef pair<int,int>P;
const int len=2e3+4;
char d[len][len];
int f(int n)//求第n个图像的中心坐标 
{
	return (pow(3,n-1)-1)/2+1;
}
void dfs(int n,int x,int y)
{
	if(n==1)
	{
		d[x][y]='X';
		return ;
	}
	int z=pow(3,n-2);//你会发现第n个图像与第n-1个图像的中心坐标x或y差z 
	dfs(n-1,x-z,y-z);//上下左右中 的中心坐标 
	dfs(n-1,x-z,y+z);
	dfs(n-1,x+z,y-z);
	dfs(n-1,x+z,y+z);
	dfs(n-1,x,y);
}
int main()  
{  
	int n;
	while(cin>>n&&n!=-1)
	{
		memset(d,' ',sizeof(d));
		dfs(n,f(n),f(n));
		int x=pow(3,n-1);
		for(int i=1;i<=pow(3,n-1);++i)
		{
			int y=pow(3,n-1)+1;//pow好像是返回double型的 
			d[i][y]='\0';//后面没有多余空格 
			printf("%s\n",d[i]+1);//看图发现第一列都是空格,不输出 
		}
		puts("-");
	}
}  

猜你喜欢

转载自blog.csdn.net/hutwuguangrong/article/details/80483586