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. VS2013 快捷键 VS RESHARPER 设置

    一直用Resharper插件,最近发现Ctrl+E,C快捷见被Resharper的快捷功能吞掉了,折腾了几小时终于找到解决方法了,特记分享之. 采用如下步骤可以让快捷键回到vs2012的默认方式,同时 ...

  2. 关于eclipse(64位)下aptana插件安装报错问题解决

    最近一直没有写过js,换了新电脑以后,eclipse下的aptana插件也没有装过,这几天要写js想重新把aptana装上,但是不知怎的,link方式.在线安装方式还有离线包下载下来利用eclipse ...

  3. linux杂记(十)what is BASH Shell

    first,what is shell?其实只要是碰过计算机的,对于OS(Operation System操作系统,不管是linux.unix.windows)有点概念的人大多都听过这个名词,因为只要 ...

  4. C#学习日志 day 2 plus ------ hyper-V 开启方法

    hyper-V的开启需要两个步骤. 第一是在bios中开启 virtualization technology--虚拟化技术 在process setting中改为enabled. 进入bios界面的 ...

  5. JAVA并发,经典死锁案例-哲学家就餐

    转自:http://blog.csdn.net/tayanxunhua/article/details/38691005 死锁经典案例:哲学家就餐. 这个案例会导致死锁. 通过修改<Java编程 ...

  6. css多行文本垂直居中问题研究

    css多行文本垂直居中问题研究 <body> <h2>垂直居中对齐</h2> <style> *{margin:0; padding:0;} div { ...

  7. Oracle的TPCC测试,原来也是个作弊的东西...

    http://www.oaktable.net/content/sorted-hash-clusters-rip 根据Jonathan Lewis老先生的测试实例,发觉cluster 的sort功能, ...

  8. linux系统巡检脚本shell实例

    #!/bin/sh BACKUP_TIMESTAMP=`date +%Y%m%d` HOSTNAME=`hostname` num=89 ###################核查文件系统opt### ...

  9. Windows Azure 新上线网络相关服务

     动态路由网关.点到站点(Point to Site)VPN正式商用 动态路由网关和点到站点VPN支持基于路由的VPN,并且允许用户将独立计算机连接到Azure上的虚拟网络.现在,虚拟网络中的动态 ...

  10. PowerDesigner 基础使用

    建表使用基础 1.打开软件,点击create model(如下图一)or 右上角 文件→建立新模型 or 快捷键 Ctrl+N(如下图二) 2.选择要生成脚本的数据库类型(见上图二) 3.建表(图三) ...