***牛客网,getline莫名其妙就把字符串末尾的空格吞掉了
迭代法做,逐次刷新map
慢归慢,胜在思路简单
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
void FillMapZoneWith(char c,char* map,int rowStart,int rowEnd,int colStart,int colEnd){
for(int i=rowStart;i<=rowEnd;i++){
for(int j=colStart;j<=colEnd;j++){
map[i*3000+j] = c;
}
}
}
void FillMapZoneWithBlank(char* map,int rowStart,int rowEnd,int colStart,int colEnd){
FillMapZoneWith(' ',map,rowStart,rowEnd,colStart,colEnd);
}
void FillMapZoneWithTemplate(char* map,char* Template,int templateSize,int rowStart,int colStart){
for(int i=0;i<templateSize;i++){
for(int j=0;j<templateSize;j++){
map[(rowStart+i)*3000+colStart+j] = Template[i*5+j];
}
}
}
void FillMap(char* &map,int mapSize,char* Template,int templateSize){
char* newMap = (char*)malloc(sizeof(char)*3000*3000);
FillMapZoneWithTemplate(newMap,map,mapSize,0,0);
//扫描map
for(int i=0;i<mapSize;i++){
for(int j=0;j<mapSize;j++){
int rowStart = i*templateSize;
int rowEnd = (i+1)*templateSize-1;
int colStart = j*templateSize;
int colEnd = (j+1)*templateSize-1;
if(map[i*3000+j]==' '){
FillMapZoneWithBlank(newMap,rowStart,rowEnd,colStart,colEnd);
} else {
FillMapZoneWithTemplate(newMap,Template,templateSize,rowStart,colStart);
}
}
}
map = newMap;
}
int main(){//习题2.4 北京大学 Repeater
int templateSize;
int scale;
char* Template = (char*)malloc(sizeof(char)*25);
string line;
while(cin>>templateSize){
if(templateSize==0) break;
cin.ignore();//吃掉\n
//生成模版阵列
for(int i=0;i<templateSize;i++){
getline(cin,line);
for(int j=0;j<templateSize;j++){
if(line.size()<j+1){
Template[i*5+j] = ' ';//***牛客网,getline能把字符串末尾的空格吞掉
} else {
Template[i*5+j] = line[j];
}
}
}
//根据模版阵列生成字符阵列
cin>>scale;
int mapSize = pow(templateSize,scale);
char* map = (char*)malloc(sizeof(char)*3000*3000);
FillMapZoneWithTemplate(map,Template,templateSize,0,0);
for(int i=1;i<scale;i++)
FillMap(map,mapSize,Template,templateSize);
//打印字符阵列
for(int i=0;i<mapSize;i++){
for(int j=0;j<mapSize;j++){
cout<<map[i*3000+j];
}
cout<<endl;
}
}
return 0;
}

京公网安备 11010502036488号