Fractal(递归,好题)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 8341 | Accepted: 3965 |
Description
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
Output
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(递归,好题)的更多相关文章
- 递归一题总结(OJ P1117倒牛奶)
题目: 农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数,最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另 ...
- 函数递归简单题-hdoj-2044 2018-一只小蜜蜂 母牛的故事
题目:一只小蜜蜂 递归做法: #include<cstdio> #include<iostream> #include<stdlib.h> #include< ...
- poj 2083 Fractal 递归 图形打印
题目链接: http://poj.org/problem?id=2083 题目描述: n = 1时,图形b[1]是X n = 2时,图形b[2]是X X X ...
- 洛谷P1427 小鱼的数字游戏 题解 递归入门题
题目链接:https://www.luogu.com.cn/problem/P1427 题目大意: 给你一串数(输入到0为止),倒序输出这些数. 解题思路: 首先这道题目可以用数组存数据,然后输出. ...
- 十五 链表与递归,leetCode203题
两种方式: package com.lt.datastructure.LinkedList; /** * leetCode 203题 * /** * Definition for singly-lin ...
- 一道Postgresql递归树题
转载请注明出处: https://www.cnblogs.com/funnyzpc/p/13698249.html 也是偶然的一次,群友出了一道题考考大家,当时正值疫情最最严重的三月(借口...),披 ...
- poj 1941 The Sierpinski Fractal 递归
//poj 1941 //sep9 #include <iostream> using namespace std; const int maxW=2048; const int maxH ...
- HDU 2563 统计问题(递归,思维题)
统计问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- POJ 2083 Fractal 分形题目
这两天自学了一线算法导论里分治策略的内容,秉着只有真正投入投入编程,才能更好的理解一种算法的思想的想法,兴致勃勃地找一些入门的题来学习. 搜了一下最后把目光锁定在了Poj fractal这一个题上.以 ...
随机推荐
- 兼容现有jQuery API的轻量级JavaScript库:Zepo
Zepo是一个JavaScript框架,其特点是兼容现有jQuery API的同时,自身体积十分小:它与jQuery有着类似的API.如果你会jQuery,那么也就会使用Zepto了. $('div' ...
- 实验时css层叠样式表不更新的情况
自定义了CSS的样式,希望在页面中起作用.因为MVC中Views/Shared/_Layout.cshtml是所有视图的公共文件,如下: <!DOCTYPE html> <html& ...
- Advanced Customization of the jQuery Mobile Buttons | Appcropolis
Advanced Customization of the jQuery Mobile Buttons | Appcropolis Advanced Customization of the jQue ...
- Yii2简单纪要
网上经常拿Yii来类比ROR,从MVC角度,使用体验及代码风格上确实有很多相似的地方.不过看配置文件发现Yii2不止是受rails的影响,同样有不少spring的影子,最明显的就是配置文件中很多IOC ...
- Primefaces 中e.offset(...)问题的处理
问题 在使用Primefaces构建的页面中,原来好好的页面莫名奇异的出现下拉框不能显示数据且点击没有反应的情况.后来通过firefox发现其JS抛出了一个e.offset(...)错误 解决方法 经 ...
- C++学习笔记29,引用变量(1)
引用变量在创建的时候就必须初始化.无法创建一个未被初始化的引用. #include <iostream> using namespace std; int main() { int x=1 ...
- linux线程之pthread_join
pthread_join使一个线程等待另一个线程结束. 代码中如果没有pthread_join:主线程会很快结束从而使整个进程结束,从而使创建的线程没有机会开始执行就结束了.加入pthread_joi ...
- 2.词法结构-JavaScript权威指南笔记
今天是第二章.所谓词法结构(lexical structure),就是写代码中最基本的东西,变量命名,注释,语句分隔等,这是抄书抄的... 1.字符集,必须是Unicode,反正Unicode是ASC ...
- 关于javascript的沙箱模式以及缓存方法
在javascript函数代码中,经常会不经意出现全局变量,很可能造成对全局对象的污染,由于这种弊端的存在,那么沙箱模式油然而生.沙箱模式又称为沙盒模式.隔离模式.在js中只有函数可以限定变量作用域, ...
- AsyncSocket的长连接使用
使用背景:需要跟服务器长期保持连接进行即时通讯:还有在跟智能硬件建立实时链接进行同步智能硬件的状态等,最近我就做项目就碰到需要实时更新智能硬件的状态(比如智能硬件的电量,以及其它工作状态),跟智能硬件 ...