Recursive algorithm--box FRACTAL box fractals (POJ2083)

Source: Internet
Author: User
Tags pow

Problem

The box fractal is defined as follows:
The 1-degree box fractals are:
X
The 2-degree box fractals are:
x x
X
x x

If B (n-1) represents the box fractal of the n-1 degree, then the box fractal recursion of the n degree is defined as follows:

B (n-1) b (n-1)
B (n-1)
B (n-1) b (n-1)

Please draw an N-degree box fractal shape

Input

Each row gives a positive integer that is not greater than 7. The last line entered ends with-1 for the input

Output

For each test case, a box fractal marked with ' X ' is written out. After each test case, the output contains a line with a dash "-".

Analysis

The size of the box fractal of n degrees is 3^ (n-1), that is, the box fractal of n degrees is a square with a width of 3^ (n-1).
Set the recursive function Printbox (n,x,y) to generate an N-degree box fractal with coordinates (x, y) as the upper-left corner.

1) Recursive boundary: If n=1, Output ' x ' at (x, y)
2) If n>1, the size of the box to calculate n-1 degrees M = 3^ (n-2), respectively, in the upper left, upper right, middle, lower left and bottom right to draw 5 n-1 degrees of box.
For the N-1 box at the top left, the coordinates of the upper-left corner are (x, y), and the recursive Printbox (n-1,x,y) is generated;
For the box with the N-1 degree in the upper right, the coordinates of the upper left corner are (x+2m,y), the recursive Printbox (n-1,x+2m,y) is generated;
For the Middle n-1 box, the coordinates of the upper left corner are (x+m,y+m), the recursive Printbox (n-1,x+m,y+m) is generated;
For the lower left n-1 box, the upper-left corner coordinates are (x,y+2m), and the recursive Printbox (n-1,x,y+2m) is generated;
For the box with n-1 degree in the upper right, the coordinates of the upper left corner are (x+2m,y+2m), the recursive Printbox (n-1,x+2m,y+2m) is generated;

Encoding implementation
#include "stdafx.h"#include <cmath>#include <iostream>using namespace STD;//7-degree box fractal largest n=3^6=729#define MAX 730CharMaps[max][max];voidPrintbox (intNintXintY) {//Recursive boundary    if(n = =1) {Maps[x][y] =' X '; }Else{size of//n-1 box fractal m        intm =POW(3N2);///n-1 box Fractal at upper leftPrintbox (N-1, x, y);///n-1 box fractals on top rightPrintbox (n1, x+2*m, y);//N-1 in the middle of the box fractalPrintbox (N-1, X, Y +2* m);///n-1-box fractals at lower leftPrintbox (N-1, X + M, y + m);//n-1-box fractals at lower rightPrintbox (n1, x+2*m,y+2*M); }}int_tmain (intARGC, _tchar* argv[]) {intn;Cin>> N; while(N! =-1){intSize =POW(3N1);//Initialize         for(inti =0; i < size; i++) { for(intj =0; J < size; J + +) {Maps[i][j] ="'; Maps[i][size] =' + '; }} printbox (N,0,0);//Output         for(inti =0; i < size; i++)printf("%s\n", Maps[i]);cout<<"-"<<endl;Cin>> N; }return 0;}
Test

Recursive algorithm--box FRACTAL box fractals (POJ2083)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.