Recursive Algorithm -- box fractal (POJ2083)
Problem
Box fragment is defined as follows:
The box fractal of 1 degree is:
X
The box fractal of 2 degrees is:
X
X
X
If B (n-1) represents the box fragment of N-1 degrees, the box fragment of n degrees is recursively defined as follows:
B (n-1) B (n-1)
B (n-1)
B (n-1) B (n-1)
Draw a box fractal graph with n degrees
Input
Each row provides a positive integer not greater than 7. The last input line uses-1 to indicate that the input has ended.
Output
For each test case, the box fragment marked with 'X' is published. After each test case, output a line containing a hyphen.
Analysis
The box fragment of n degrees is 3 ^ (n-1), that is, the box fragment of n degrees is a square with a length and width of 3 ^ (n-1.
Set the recursive function printBox (n, x, y) to generate the n-degree box fragment in the upper left corner of the coordinate (x, y.
1) recursive boundary: If n = 1, 'X' is output in (x, y'
2) if n> 1, then calculate n-1 degrees of the box size m = 3 ^ (n-2), respectively in the upper left, upper right, middle, 5 n-1 boxes are drawn at the bottom left and bottom right.
For the box with n-1 degrees in the upper left corner, the coordinates in the upper left corner are (x, y), recursively generated by printBox (n-1, x, y;
For the box with n-1 degrees in the upper right corner, the coordinates in the upper left corner are (x + 2 m, y), recursively generated by printBox (n-1, x + 2 m, y;
For boxes with n-1 degrees in the middle, the coordinates in the upper left corner are (x + m, y + m), recursively generated by printBox (n-1, x + m, y + m;
For the box with n-1 degrees in the lower left, the coordinates in the upper left corner are (x, y + 2 m), recursively generated by printBox (n-1, x, y + 2 m;
For boxes with n-1 degrees in the upper right corner, the coordinates in the upper left corner are (x + 2 m, y + 2 m), recursively generated by printBox (n-1, x + 2 m, y + 2 m;
Coding implementation
# Include "stdafx. h" # include
# Include
Using namespace std; // 7 degree box fragment maximum scale n = 3 ^ 6 = 729 # define MAX 730 char maps [MAX] [MAX]; void printBox (int n, int x, int y) {// recursive boundary if (n = 1) {maps [x] [y] = 'X ';} else {// n-1 degree box fragment scale m int m = pow (3, n-2); // n-1 degree box fragment in the upper left corner printBox (n-1, x, y); // The n-1 degrees box fragment printBox (n-1, x + 2 * m, y) in the upper right corner; // The n-1 degrees box fragment printBox (n-1, x, y + 2 * m); // The n-1 degrees box fractal printBox (n-1, x + m, y + m) in the lower left ); // The n-1 degrees box fractal printBox (n-1, x + 2 * m, y + 2 * m) in the lower right corner ); } Int _ tmain (int argc, _ TCHAR * argv []) {int n; cin> n; while (n! =-1) {int size = pow (3, n-1); // initialize for (int I = 0; I <size; I ++) {for (int j = 0; j <size; j ++) {maps [I] [j] = ''; maps [I] [size] = '\ 0';} printBox (n, 0, 0); // output for (int I = 0; I <size; I ++) printf ("% s \ n", maps [I]); cout <"-" <
> N;} return 0 ;}
Test