模版生成图片第七题

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 NN (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  

解决代码:

#include<iostream>
#include<cstdio>
#include<math.h>
#include<string>

using namespace std;

    
    char templat [6][6]; //初始模版:
    char m2[3000][3000];// new template
    char result[3000][3000]; 
    int len; // new the size of matrix
    int n,q; // input n and q level

void update(int x,int y,bool flag) {
        // len 应该是此时(模版)的大小
        for (int i = 0;i < len; i++)
        {
            for (int j = 0; j < len; j++)
            {
                if (flag)
                result [x+i][y+j] = m2[i][j]; // 每次更新一个模版大小的图形
                else 
                result [x+i][y+j] = ' ';
            }
            
        }
        
}

int main(){
    while(scanf("%d",&n)){
        // input and output is not in same buffer so,every time you can output the result array;
            if(0==n) break;
            //2.前面的scanf()在读取输入时会在缓冲区中留下一个字符'\n'(输入完s[i]的值后按回车键所致),
            //所以如果不在此加一个getchar()把这个回车符取走的话,gets()就不会等待从键盘键入字符,
            //而是会直接取走这个“无用的”回车符,从而导致读取有误;
            getchar(); // take the \n
            for(int i=0;i<n;i++){
                //读入整行数据,它使用回车键输入的换行符来确定输入结尾。
                //调用方法: cin.getline(str (字符数组), len);
            
            // 获取输入方法2:
            // string temp;
            // getline(cin,temp);
            // for (int j = 0; j < temp.size(); j++)
            //     templat[i][j] = temp[j];

            cin.getline(templat[i],6); // 获取输入方法1

            }
            
            scanf("%d",&q);
            for(int k=0;k<q;k++){
                len = pow(n,k);
                // first level:
                if(1==len){ // 边界条件 如果是k就是0,如果是len就是1
                    for (int i = 0; i < n; i++)
                        for(int j=0;j<n;j++){
                            m2[i][j] = templat[i][j];
                            result[i][j] = m2[i][j];
                        }
                }
                else{
                    // 1.分析模版,按照初始模版来替换 
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            if (templat[i][j]==' ')
                            update(i*len,j*len,false);
                            else 
                            update(i*len,j*len,true);
                        }
                        
                    }
                    // 3. 交换 此时的 模版和结果:
                    for (int i = 0; i < len*n; i++){
                    for (int j = 0; j < len*n; j++)
                        m2[i][j] = result[i][j];
                     }

                }
            }

        //输出
        len = pow(n,q);
        for (int i = 0; i < len; i++)
        {
            for (int j = 0; j < len; j++)
                cout << result[i][j];
                 
            cout << endl;
        }

    }
    return 0;
}

总结:

  1. 先构造结果的matrix 然后再输出
  2. 每一个存在换成模版
  3. 注意每个level的 判断
  4. c++基础知识,从命令行读入一段字符串。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值