递归遍历——以Repeater问题为例

本文通过一个复杂的构造型枚举问题——Repeater,探讨了如何利用递归进行解决。重点在于理解递归思想,并提供了一个C语言实现的例子。递归在解决图形类排版问题上具有重要作用,作者推荐使用C语言来练习,例如通过编写贪吃蛇游戏进行综合训练。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:
Harmony is indispensible in our daily life and no one can live without it----may be Facer is the only exception. One day it is rumored that repeat painting will create harmony and then hundreds of people started their endless drawing. Their paintings were based on a small template and a simple method of duplicating. Though Facer can easily imagine the style of the whole picture, but he cannot find the essential harmony. Now you need to help Facer by showing the picture on computer. You will be given a template containing only one kind of character and spaces, and the template shows how the endless picture is created----use the characters as basic elements and put them in the right position to form a bigger template, and then repeat and repeat doing that. Here is an example. # # # <-template # # So the Level 1 picture will be # # # # # Level 2 picture will be # # # # # # # # # # # # # # # # # # # # # # # # #
输入:
The input contains multiple test cases. The first line of each case is an integer N, representing the size of the template is N*N (N could only be 3, 4 or 5). Next N lines describe the template. The following line contains an integer Q, which is the Scale Level of the picture. Input is ended with a case of N=0. It is guaranteed that the size of one picture will not exceed 3000*3000.
输出:
For each test case, just print the Level Q picture by using the given template.
样例输入:
3
# #
#
# #
1
3
# #
#
# #
3
4
OO
O O
O O
OO
2
0
样例输出:

本题较为复杂,属于构造型枚举,但是构造过程中需要递归,因此,将递归操作单独编写一个draw函数。本题较难,但是其递归思想很重要,必须掌握。

C语言实现:

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

int N, Q;
char t[6][6];
char p[3000][3001];

void draw(int q, int x, int y) {
    int eSize = (int)pow(N, q - 1);

    int i, j;
    if (q == 1) {
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++) {
                p[x + i][y + j] = t[i][j];
            }
        }
    } else {
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++) {
                if (t[i][j] != ' ') {
                    draw(q - 1, x + eSize * i, y + eSize * j);
                }
            }
        }
    }
}

int main() {
    while (scanf("%d", &N) != EOF) {
        if (N == 0 || N > 5) {
            break;
        }
//        去掉多余的换行符
        getchar();
//        输入模板
        int i, j;
        for (i = 0; i < N; i++) {
            gets(t[i]);
        }
        scanf("%d", &Q);
        int size = (int)pow(N, Q);
//        格式化图画
        for (i = 0; i < size; i++) {
            for (j = 0; j < size; j++) {
                p[i][j] = ' ';
            }
            p[i][size] = '\0';
        }
        draw(Q, 0, 0);
        for (i = 0; i < size; i++) {
            puts(p[i]);
        }

    }
    return 0;
}

想要综合训练图形类排版问题,建议用C语言做一个贪吃蛇。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值