一、题目内容
题目描述:
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
二、代码及注释
#include<cstdio>
#include<string>
#include<cmath>
using namespace std;
char temp[6][6];
char buf[3001][3001];
int n,q;
//level用于判断递归层数,x,y为起始坐标
void update(int level,int x,int y){
//若层数为1,则直接重复原来的图像
if(level==1){
for(int i=x;i<x+n;i++){
for(int j=y;j<y+n;j++){
buf[i][j]=temp[i-x][j-y];
}
}
}else{
int n1=pow(n,level);
int n2=pow(n,level-1);
for(int i=x;i<x+n1;i+=n2){
for(int j=y;j<y+n1;j+=n2){
if(temp[(int)((i-x)/n2)][(int)((j-y)/n2)]!=' '){//如果原来图像不为空格,那么进行复制
update(level-1,i,j);
}
}
}
}
}
int main(){
while(scanf("%d",&n)!=EOF){
if(n==0) break;
getchar();//这里注意,因为下边要使用gets,gets会把enter当做一行,所以这里先getchar一下
for(int i=0;i<n;i++){
gets(temp[i]);
}
scanf("%d",&q);
int sqrt=pow(n,q);//这里先计算一下,否则下边报错
for(int i=0;i<sqrt;i++){
for(int j=0;j<sqrt;j++){
buf[i][j]=' ';
}
}
update(q,0,0);
for(int i=0;i<sqrt;i++){
for(int j=0;j<sqrt;j++){
printf("%c",buf[i][j]);
}
printf("\n");
}
}
return 0;
}