【学习笔记】〖九度OJ〗题目1161:Repeater

2020-10-18 05:03发布

题目1161:Repeater

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:682

解决:225

题目描述:

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年北京大学计算机研究生机试真题
答疑:
解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7884-1-1.html
本题要求按模板将原图扩展,扩展方法是,如果在模板中当前位置(i, j)不为空格,那么再下一层的图形中,相对位置(i,j)处(即按比例扩大)为本层的完整图形。


题目带有递归性质,但递归效率低,改为迭代实现。


新方法,两个缓冲区轮换使用的方法,详见代码



#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
 
char temp[3001][3001];
char res1[3001][3001];
char res2[3001][3001];
 
void tempcopy(char source[3001][3001], char target[3001][3001], /*target的首坐标*/int r, int c, int n)
{
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
        {
            target[r+i][c+j] = source[i][j];
        }
    }
}
//模板,长度,上一层模板,长度,结果,特殊字符,层数
void nextLevel(char temp[3001][3001], int n, char module[3001][3001], char res[3001][3001], char c, int l)
{
    //清空RES
    int mmax = pow((double)n, (double)l);
     
    int i,j;
    for (i=0; i<mmax; i++)
    {
        for (j=0; j<mmax; j++)
        {
            res[i][j] = ' ';
        }
    }
    int bias = pow((double)n, (double)l-1);
    for (i=0; i<n; i++)
    {
        for (j=0; j<n; j++)
        {
            if (temp[i][j] == c)
            {
                tempcopy(module, res, i*bias, j*bias, bias); 
            }
        }
    }
    int max = l*n;
    for (i=0; i<max; i++)
    {
        for (i=0; i<max; i++)
        {
            if (res[i][j] != c)
            {
                res[i][j] = ' ';
            }
        }
    }
}
 
int main()
{
     
    int n, q, i, j;
 
    while(cin >> n && n!=0)
    {
        for (i=0; i<7; i++)
        {
            for (j=0; j<7; j++)
            {
                temp[i][j] = ' ';
         
            }
        }
        char c=' ';
        cin.get();
        for (i=0; i<n; i++)
        {
            cin.getline(temp[i],n+1);
        }
         
        cin >> q;
        int max = pow((double)n,(double)q);
        //清空
        for (i=0; i<max; i++)
        {
            for (j=0; j<max; j++)
            {
                res1[i][j] = ' ';
                res2[i][j] = ' ';
            }
        }
        //复制temp用于第一次扩展去特殊化
        for (i=0; i<n; i++)
        {
            for (j=0; j<n; j++)
            {
                res1[i][j] = temp[i][j];
            }
        }
        //确定tmp中的特殊字符
        for (i=0; i<n; i++)
        {
            if (temp[0][i] != ' ')
            {
                c = temp[0][i];
                break;
            }
        }
 
        //扩展
        for (i=2; i<=q; i++)
        {
             
            if (i%2 != 0)//奇次
            {
                nextLevel(temp, n, res2, res1, c, i);
            }
            else//偶次
            {
                nextLevel(temp, n, res1, res2, c, i);
            }
        }
         
        if (i%2 != 0)
        {
            for (i=0; i<max; i++)
            {
             
                for (j=0; j<max; j++)
                {
                 
                    printf("%c", res2[i][j]);
                }
                printf("\n");
            }
         
        }
        else
        {
            for (i=0; i<max; i++)
            {
             
                for (j=0; j<max; j++)
                {
                 
                    printf("%c", res1[i][j]);
                }
                printf("\n");
            }
         
        }
         
    }
     
    return 0;
}



标签: