zoukankan      html  css  js  c++  java
  • 九度OJ 1161:Repeater(复制器) (递归)

    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:1449

    解决:508

    题目描述:

    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
    样例输出:
    # #
     # 
    # #
    # #   # #         # #   # #
     #     #           #     # 
    # #   # #         # #   # #
       # #               # #   
        #                 #    
       # #               # #   
    # #   # #         # #   # #
     #     #           #     # 
    # #   # #         # #   # #
             # #   # #         
              #     #          
             # #   # #         
                # #            
                 #             
                # #            
             # #   # #         
              #     #          
             # #   # #         
    # #   # #         # #   # #
     #     #           #     # 
    # #   # #         # #   # #
       # #               # #   
        #                 #    
       # #               # #   
    # #   # #         # #   # #
     #     #           #     # 
    # #   # #         # #   # #
         OO  OO     
        O  OO  O    
        O  OO  O    
         OO  OO     
     OO          OO 
    O  O        O  O
    O  O        O  O
     OO          OO 
     OO          OO 
    O  O        O  O
    O  O        O  O
     OO          OO 
         OO  OO     
        O  OO  O    
        O  OO  O    
         OO  OO     
    来源:
    2011年北京大学计算机研究生机试真题

    思路:

    开始以为这个题很难,后来发现起始就是基于模板画图,用递归来做就行。

    容易出细节错误。


    代码:

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
     
    #define N 3000
    #define M 5
     
    char p[N][N+1];
    char tem[M][M+1];
    int m;
     
    void cp(int x, int y)
    {
        int i, j;
        for (i=0; i<m; i++)
        {
            for (j=0; j<m; j++)
            {
                p[i+x][j+y] = tem[i][j];
            }
        }
    }
     
    void set(int n, int x, int y)
    {
        if (n == 1)
        {
            cp(x, y);
            return;
        }
        int i, j;
        int size = pow(m, n-1);
        for (i=0; i<m; i++)
        {
            for (j=0; j<m; j++)
            {
                if (tem[i][j] != ' ')
                    set(n-1, x+i*size, y+j*size);
            }
        }
    }
     
    void print(int n)
    {
        int size = pow(m, n);
        for (int i=0; i<size; i++)
        {
            for (int j=0; j<size; j++)
            {
                printf("%c", p[i][j]);
            }
            printf("
    ");
        }
    }
     
    int main(void)
    {
        int n, i, j;
        while (scanf("%d", &m) != EOF && m)
        {
            getchar();
            for (i=0; i<N; i++)
            {
                for (j=0; j<N; j++)
                    p[i][j] = ' ';
                p[i][N] = '';
            }
            for (i=0; i<m; i++)
            {
                gets(tem[i]);
            }
            scanf("%d", &n);
            getchar();
            set(n, 0, 0);
            print(n);
        }
     
        return 0;
    }
    /**************************************************************
        Problem: 1161
        User: liangrx06
        Language: C
        Result: Accepted
        Time:260 ms
        Memory:9796 kb
    ****************************************************************/


    编程算法爱好者。
  • 相关阅读:
    字符串及课堂例子整理
    大道至简:软件工程实践者的思想——第四章感想
    大道至简:软件工程实践者的思想——第一章感想(重写)
    大道至简:软件工程实践者的思想——第三章感想
    大道至简:软件工程实践者的思想——第二章感想和课后思考
    简单程序代码及心得体会
    《大 道 至 简   ——软件工程实践者的思想 》是懒人造就了方法读后感
    实验报告
    懒人的思考造就了方法
    编程其实很简单
  • 原文地址:https://www.cnblogs.com/liangrx06/p/5083866.html
Copyright © 2011-2022 走看看