九度OJ 1161 Repeater

    题目1161:Repeater(题目转载http://blog.youkuaiyun.com/j597960549/article/details/20560863)

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:682

解决:225

题目描述:

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     
来源:
2011年北京大学计算机研究生机试真题
思路:采用双缓冲区思想,交替作为模板复制,学习了(http://blog.youkuaiyun.com/j597960549/article/details/20560863)的思想 代码如下:
#include <iostream>
#include <stdio.h>
#include <math.h> 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

char model[5][5];
char buff1[3000][3000];
char buff2[3000][3000];//双缓冲区 
void tempcopy(char source[3000][3000],char target[3000][3000],int x,int y,int n)//X,Y为target起始坐标,n为模板的边长 
{
     int i,j;
	 for(i=0;i<n;i++)
	 {
	 	for(j=0;j<n;j++)
	 	{
	 		target[x+i][y+j]=source[i][j];
	 	}
	 } 
} 
void nextlevel(char model[5][5],char lastresult[3000][3000],char result[3000][3000],int n,int l)//model为初始模板,lastresult为上一级的图像,result为本级的结果图像,n为初始规模,l为本次图像级别 
{
	int maxSize=pow((double)n,(double)l);
	int i,j;
	//初始化 
	for(i=0;i<maxSize;i++)
	{
		for(j=0;j<maxSize;j++)
		{
			result[i][j]=' ';
		}
	}
	int diff=pow((double)n,(double)(l-1));//间距 ,也是上一级图像的边长 
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			if(model[i][j]!=' ')
			{
				tempcopy(lastresult,result,i*diff,j*diff,diff);//模板位置不为空格的位置,在扩大图的对应位置,复制为上一级的图像 
			}
		}
	}
	
	
	
	
}

int main(int argc, char** argv) {
	
	int n,q;
	int i,j;
	while(cin>>n && n!=0)
	{   
	    for(i=0;i<5;i++)
	    {
	    	for(j=0;j<5;j++)
	    	{
	    		model[i][j]=' ';
	    	}
	    }
	    cin.get();//读入换行符 
		for(i=0;i<n;i++)
		{
		
				cin.getline(model[i],n+1);
		
		}
    
	    cin>>q;//输入级数
		
		//初始化缓冲区1 ,若级别为1,则缓冲区1即为最终结果,以保证下面的循环是缓冲区1和缓冲区2 的相互模仿扩大, 
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				buff1[i][j]=model[i][j];
			}
		}
		
		int p =2 ;//记录当前扩大图像的级别,排除第一次 
	
	while(p<=q)
	{
		if(p%2==0)
		{
			nextlevel(model,buff1,buff2,n,p);//若为偶次,缓冲区1为模板 
		}
		else
		{
			nextlevel(model,buff2,buff1,n,p);//若为奇数次,缓冲区2为模板 
		}	
		p++;
	}
	 
	 int MaxSize=pow((double)n,(double)q);//图像最终的最大规模 
	 
	if(q%2==1)
	{
		for(i=0;i<MaxSize;i++)
		{
			for(j=0;j<MaxSize;j++)
			{
					cout<<buff1[i][j];
			}
				cout<<endl;
		}
	} 
	else
	{
			for(i=0;i<MaxSize;i++)
		{
			for(j=0;j<MaxSize;j++)
			{
			cout<<buff2[i][j];
			}
			cout<<endl;
		}
	}
}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值