poj 2083 Fractal 递归 图形打印
题目链接:
http://poj.org/problem?id=2083
题目描述:
n = 1时,图形b[1]是X
n = 2时,图形b[2]是X X
X
X X
所以n时,图形b[n]是b[n-1] b[n-1]
b[n-1]
b[n-1] b[n-1]
解题思路:
用递归打印图形,存到二维数组里面,输出是一个矩形,竟然是一个矩形!!!!!!
代码:
1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 using namespace std; 6 #define maxn 740 7 char map[maxn][maxn]; 8 9 void dfs (int n, int x, int y); 10 //n是b[n],(x,y)是b[n]左上那个点; 11 int main () 12 { 13 int i, j, n; 14 while (scanf ("%d", &n), n != -1) 15 { 16 int m = (int)pow(3,n-1); 17 for (i=0; i<=m; i++) 18 { 19 for (j=0; j<=m; j++) 20 map[i][j] = ' '; 21 map[i][m+1] = '\0'; 22 } 23 dfs (n, 0, 0); 24 for (i=0; i<m; i++) 25 puts(map[i]); 26 printf ("-\n"); 27 } 28 return 0; 29 } 30 31 void dfs (int n, int x, int y) 32 { 33 if (n == 1) 34 { 35 map[x][y] = 'X'; 36 return ; 37 } 38 int m = (int) pow (3, n-2);//b[n-1]的大小 39 dfs (n-1, x, y);//up_left 40 dfs (n-1, x, y+2*m);//up_right 41 dfs (n-1, x+m, y+m);//middle 42 dfs (n-1, x+2*m, y);//down_left 43 dfs (n-1, x+2*m, y+2*m);//down_right 44 }
本文为博主原创文章,未经博主允许不得转载。