北大考研复试上机——Repeater

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.
示例1

输入

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     

    思路:用一个二维数组mould储存模板,这个模板就是每次repeat使用的。用temp储存repeat的中间过程,用picture储存repeat的结果。这里有一点需要注意,repeat的数目是输入的q的数目减一。

    repeat的过程是这样的,遍历模板,在模板是空格的地方,在picture相应的位置放一个与上一步repeat结果大小一样的空白区,如果模板不是空格(有图形),那就在picture相应的位置放上上一步repeat的结果图形。

    这道题很有趣味,因为题意难以理解,这道题里repeat的题意是用分形的方式,把原图形扩大一下,就是把原图形看做一个元素,在模板有图形的地方用这个元素替换。设模板大小是m_size,每次repeat,最终图形的大小变成原来大小的m_size倍。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

char mould[7][7];
char picture[3005][3005];
char temp[3005][3005];

void duplicate(int m_size, int p_left, int p_top)
{
    for(int i = 0; i < m_size; i++)
    {
        for(int j = 0; j < m_size; j++)
        {
            picture[p_left + i][p_top + j] = temp[i][j];
        }
    }
}

void enblank(int m_size, int p_left, int p_top)
{
    for(int i = 0; i < m_size; i++)
    {
        for(int j = 0; j < m_size; j++)
        {
            picture[p_left + i][p_top + j] = ' ';
        }
    }
}

int repeater(int m_size, int repeater_times)
{
    int n = m_size;
    while(--repeater_times)
    {
        for(int i = 0; i < m_size; i++)
        {
            for(int j = 0; j < m_size; j++)
            {
                if(mould[i][j] != ' ')
                    duplicate(n, n*i, n*j);
                else
                    enblank(n, n*i, n*j);
            }
        }
        n *= m_size;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                temp[i][j] = picture[i][j];
            }
        }
    }
    return n;
}

int main()
{
    int n, q;
    while((scanf("%d", &n) != EOF) && n != 0)
    {
        getchar();
        for(int i = 0; i < n; i++)
        {
            gets(mould[i]);
            strcpy(temp[i], mould[i]);
            strcpy(picture[i], mould[i]);
        }
        scanf("%d", &q);
        getchar();
        n = repeater(n, q);
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                printf("%c", picture[i][j]);
            }
            printf("\n");
        }
    }
}



你可能感兴趣的:(c/c++)