Fractal
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 8341   Accepted: 3965

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
  • 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 OutputX

-
X X
X
X X
-
X X X X
X X
X X X X
X X
X
X X
X X X X
X X
X X X X
-
X X X X X X X X
X X X X
X X X X X X X X
X X X X
X X
X X X X
X X X X X X X X
X X X X
X X X X X X X X
X X X X
X X
X X X X
X X
X
X X
X X X X
X X
X X X X
X X X X X X X X
X X X X
X X X X X X X X
X X X X
X X
X X X X
X X X X X X X X
X X X X
X X X X X X X X
-
递归:
思路很巧妙,把每个左上角的点作为起点;开始递归;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x);
char a[2500][2500];
void dfs(int x,int y,int cur){
if(cur==1){
a[x][y]='X';
return;
}
int n,m;
dfs(x,y,cur-1);
//左上
m=x;n=y+pow(3,cur-2)*2;
dfs(m,n,cur-1);
//右上
m=x+pow(3,cur-2)*2;n=y;
dfs(m,n,cur-1);
//左下
m=x+pow(3,cur-2)*2;n=y+pow(3,cur-2)*2;
dfs(m,n,cur-1);
//右下
m=x+pow(3,cur-2);n=y+pow(3,cur-2);
dfs(m,n,cur-1);
//中
}
int main(){
int N;
while(scanf("%d",&N),N!=-1){
int len=pow(3,N-1);
for(int i=0;i<len;i++){
for(int j=0;j<len;j++)
a[i][j]=' ';
a[i][len]='\0';
}
dfs(0,0,N);
for(int i=0;i<len;i++){
printf("%s\n",a[i]);
}
puts("-");
}
return 0;
}

  

 

Fractal(递归,好题)的更多相关文章

  1. 递归一题总结(OJ P1117倒牛奶)

    题目:                    农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数,最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另 ...

  2. 函数递归简单题-hdoj-2044 2018-一只小蜜蜂 母牛的故事

    题目:一只小蜜蜂 递归做法: #include<cstdio> #include<iostream> #include<stdlib.h> #include< ...

  3. poj 2083 Fractal 递归 图形打印

    题目链接: http://poj.org/problem?id=2083 题目描述: n = 1时,图形b[1]是X n = 2时,图形b[2]是X  X        X               ...

  4. 洛谷P1427 小鱼的数字游戏 题解 递归入门题

    题目链接:https://www.luogu.com.cn/problem/P1427 题目大意: 给你一串数(输入到0为止),倒序输出这些数. 解题思路: 首先这道题目可以用数组存数据,然后输出. ...

  5. 十五 链表与递归,leetCode203题

    两种方式: package com.lt.datastructure.LinkedList; /** * leetCode 203题 * /** * Definition for singly-lin ...

  6. 一道Postgresql递归树题

    转载请注明出处: https://www.cnblogs.com/funnyzpc/p/13698249.html 也是偶然的一次,群友出了一道题考考大家,当时正值疫情最最严重的三月(借口...),披 ...

  7. poj 1941 The Sierpinski Fractal 递归

    //poj 1941 //sep9 #include <iostream> using namespace std; const int maxW=2048; const int maxH ...

  8. HDU 2563 统计问题(递归,思维题)

    统计问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. POJ 2083 Fractal 分形题目

    这两天自学了一线算法导论里分治策略的内容,秉着只有真正投入投入编程,才能更好的理解一种算法的思想的想法,兴致勃勃地找一些入门的题来学习. 搜了一下最后把目光锁定在了Poj fractal这一个题上.以 ...

随机推荐

  1. hadoop搭建杂记:Linux下不同linux主机之间文件copy的scp命令

    不同的Linux之间copy文件常用有3种方法: 不同的Linux之间copy文件常用有3种方法: ①ftp 就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的程序来进行文件 ...

  2. 解决全站ie6下PNG图片不透明问题只要几行代码

    解决全站ie6下PNG图片不透明问题只要复制下面这几行代码粘贴在你的文档最底部,需要用到的包DD_belatedPNG_0.0.8a.js自己网上下载吧 代码走起 /*在文档底部加入以下代码*/ &l ...

  3. zoj 3171 The Hidden 7's

    这道题,我在网上看到两种dp,不过基本原理是一样的,不过感觉还是后面的一种比较巧妙!因为我对动态不是很熟,自能加上一些自己的理解,写上注释. 1) #include <stdio.h> # ...

  4. (Problem 19)Counting Sundays

    You are given the following information, but you may prefer to do some research for yourself. 1 Jan ...

  5. IOS 特定于设备的开发:使用加速能力“向上定位”

    iPhone提供了3个机载的传感器,用于沿着iPhone的3根相互垂直的轴(左/右(x轴).上/下(y轴)和前/后(z轴))度量加速能力.这些值指示作用于iPhone的力,它们来自重力和用户移动.可以 ...

  6. python基础学习笔记4--抽象

    抽象 1.函数: 1) 函数是可以调用,它执行某种行为并且返回一个值.可以通过callable函数来判断函数是否可调用. eg:>>> def hello(name):        ...

  7. [问题解决] File "/struts-tags" not found

    错误:org.apache.jasper.JasperException: File "/struts-tags" not found 发生场景:tomcat服务器 解决方案:将t ...

  8. mysql外键使用和级联

    如下面的: create table applicant (id int not null auto_increment primary key, jobId int not null, studen ...

  9. Android应用开发基础篇(1)-----Button

    Android应用开发基础篇(1)-----Button   一.概述        Button,顾名思义就是按钮的意思,它主要的功能是响应用户按下按钮时的动作. 二.应用      新建一个工程, ...

  10. 编译ycm库

    在安装完YCM之后,重新打开vim还会出现如下的报错信息:ycm_client_support.[so|pyd|dll] and ycm_core.[so|pyd|dll] not detected; ...