模版生成圖片第七題

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++基礎知識,從命令行讀入一段字符串。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章