北京大学の大学院受験の再試験は機に乗ります——Repeater

5071 ワード

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.# # #     
説明を入力:
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     

構想:2次元配列mouldでテンプレートを格納します.このテンプレートはrepeatのたびに使用されます.repeatの中間プロセスをtempで格納し,repeatの結果をpictureで格納した.ここで注意すべき点は、repeatの数は入力されたqの数から1を減らすことである.
repeatのプロセスは、テンプレートを遍歴し、テンプレートがスペースである場所にpictureの対応する位置に前のrepeat結果と同じ大きさの空白領域を置き、テンプレートがスペース(図形がある)でない場合はpictureの対応する位置に前のrepeatの結果図形を置く.
この問題はとても面白くて、問題の意味が理解しにくいため、この問題の中でrepeatの問題の意味は分形の方式で、元の図形を拡大して、元の図形を1つの要素と見なして、テンプレートの図形の地方でこの要素で置き換えます.テンプレートサイズを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("
"); } } }