当前位置: 代码迷 >> 综合 >> 洛谷 :lSP15728 ULM02 - The Sierpinski Fractal
  详细解决方案

洛谷 :lSP15728 ULM02 - The Sierpinski Fractal

热度:103   发布时间:2023-11-24 12:13:19.0

题目传送门<==戳这

递归画图问题:
先找大模块的规律,将其分解为较小模块同样满足此规律,此时即可运用分治思想进行画图;

n==1   /\                 能够发现,第n个形态都是由3个第n-1个形态组成(中上,左下,右下)/__\				  因此从第n个开始递归,递归边界为n==1n==2   /\/__\/\  /\/__\/__\n==3   /\/__\/\  /\/__\/__\/\      /\/__\    /__\/\  /\  /\  /\
/__\/__\/__\/__\

上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
char a[3005][3005];
const char CX='/',CY='\\',CZ='_';  ==>注意  \ 符号的表示方法void set(int lv,int sx,int sy){
        ==>递归层数,画图起点x坐标(行运动),画图起点y坐标(列运动)if(lv==1){
                         ==>手动画出n==1时候的图a[sx][sy+1]=CX;     a[sx][sy+2]=CY;a[sx+1][sy]=CX;a[sx+1][sy+1]=CZ;a[sx+1][sy+2]=CZ;a[sx+1][sy+3]=CY;}else{
    						  int k=(1<<(lv-1));         ==>三个小模块的相对位置的间隔(你细品)set(lv-1,sx,sy+k);		   ==>中上set(lv-1,sx+k,sy);		   ==>左下set(lv-1,sx+k,sy+2*k);	   ==>右下} 
}int main(){
    while(cin>>n && n){
    						==>多组输入memset(a,' ',sizeof(a));			==>初始化全部为空格,免得加空格set(n,1,1);	==>细品行和列与n的关系		for(int i=1;i<=(1<<n);i++){
    		    ==>for(int j=1;j<=(2<<n);j++){
    		==>putchar(a[i][j]);}putchar('\n');}putchar('\n');} 
}