题目描述:
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
样例输出:

本题较为复杂,属于构造型枚举,但是构造过程中需要递归,因此,将递归操作单独编写一个draw函数。本题较难,但是其递归思想很重要,必须掌握。
C语言实现:
#include <stdio.h>
#include <math.h>
int N, Q;
char t[6][6];
char p[3000][3001];
void draw(int q, int x, int y) {
int eSize = (int)pow(N, q - 1);
int i, j;
if (q == 1) {
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
p[x + i][y + j] = t[i][j];
}
}
} else {
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (t[i][j] != ' ') {
draw(q - 1, x + eSize * i, y + eSize * j);
}
}
}
}
}
int main() {
while (scanf("%d", &N) != EOF) {
if (N == 0 || N > 5) {
break;
}
// 去掉多余的换行符
getchar();
// 输入模板
int i, j;
for (i = 0; i < N; i++) {
gets(t[i]);
}
scanf("%d", &Q);
int size = (int)pow(N, Q);
// 格式化图画
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
p[i][j] = ' ';
}
p[i][size] = '\0';
}
draw(Q, 0, 0);
for (i = 0; i < size; i++) {
puts(p[i]);
}
}
return 0;
}
想要综合训练图形类排版问题,建议用C语言做一个贪吃蛇。