starlight321的博客

可以叫我菜菜,因为我真的很菜很菜很菜。。。。。。

POJ 2083 fractal

                                                     Fractal

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 11043 Accepted: 4941

Description

A fractal is an object or quantity that displays self-similarity, in a somewhat technical sense, on all scales. The object need not exhibit exactly the same structure at all scales, but the same "type" of structures must appear on all scales.
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

The input consists of several test cases. Each line of the input contains a positive integer n which is no greater than 7. The last line of input is a negative integer −1 indicating the end of input.

Output

For each test case, output the box fractal using the 'X' notation. Please notice that 'X' is an uppercase letter. Print a line with only a single dash after each test case.

Sample Input

1
2
3
4
-1

Sample Output

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 X
-

Source

题意概括:画出分形图。

解题思路:递归。找出头2~3个图的规律,然后进行递归。

首先要找出第n个图形所占的行数与列数,即找出n与行数和列数的关系。然后将整个图形初始化:即把图形所占的行数与列数的每一个位置都赋初值为空格,同时在每一行的末尾用‘\0’来结束。

然后用dfs进行深搜,将n以及第一个图形的初始位置传入dfs函数,每一次n都减小1.

AC代码:

#include<stdio.h>
#include<math.h>

char map[730][730];

void dfs(int n, int x, int y)
{
    int size2;
    if(n == 1)
    {
        map[x][y] = 'X';   
        return;    //第二出错误,忘记了结束条件应该有return。
    }
    size2 = pow(3, n - 2);
    dfs(n - 1, x, y);
    dfs(n - 1, x + 2 * size2, y);
    dfs(n - 1, x, y + 2 * size2);
    dfs(n - 1, x + size2, y + size2);
    dfs(n - 1, x + 2 * size2, y + 2 * size2);
    return;
}

int main(void)
{
    int n, i, j, size1;
    while(scanf("%d", &n) != EOF)
    {
        if(n == -1)
        {
            break;
        }
        size1 = pow(3, n - 1);
        for(i = 1; i <= size1; i ++)  //第一处错误,没有对图形进行初始化
        {
            for(j = 1; j <= size1; j ++)
            {
                map[i][j] = ' ';
            }
            map[i][j] = '\0';
        }
        dfs(n, 1, 1);
        for(i = 1; i <= size1; i ++)  
        {
            printf("%s\n", map[i] + 1);  //第三处错误,map[i] + 1为从第一列开始
        }
        printf("-\n");
    }
    return 0;
}

错误原因:

1:不知道要对图形进行初始化,尤其是输出为%s时。

2:对于输出map[i]+1没概念,其实在使用%s对图形进行输出时,如果是从第i行j列输出的话,输出应为map[i] + j。

3:如果使用双重循环和%c输出图形超时的话,可以尝试改为单重循环和%s进行输出(有可能可以避免超时)!!

4:跳出递归时忘记了写return。 敲打自己啊啊啊啊啊敲打

阅读更多
版权声明: https://blog.csdn.net/starlight321/article/details/79973364
上一篇拆分自然数
下一篇POJ 2533 Longest Ordered Subsequence
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭