repeater【递归+模拟】【难】

题目描述

和谐在我们的日常生活中是不可或有的,没有它----,也许面是唯一的例外。有一天,有传言说,重复绘画将创造和谐,然后数百人开始他们无尽的绘画。他们的绘画是基于一个小模板和一个简单的复制方法。虽然 Facer 很容易想象整个画面的风格,但他找不到本质的和谐。现在,您需要通过在计算机上显示图片来帮助 Facer。将为他们提供一个模板,仅包含一种字符和空格,模板显示如何创建无尽的图片----将字符用作基本元素,并将它们放在正确的位置以形成更大的模板,然后重复并重复这样做。下面是一个示例。* *#

输入描述:

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.

input:

3
# #
 # 
# #
1
3
# #
 # 
# #
3
4
 OO 
O  O
O  O
 OO 
2
0

 

output:

# #
 # 
# #
# #   # #         # #   # #
 #     #           #     # 
# #   # #         # #   # #
   # #               # #   
    #                 #    
   # #               # #   
# #   # #         # #   # #
 #     #           #     # 
# #   # #         # #   # #
         # #   # #         
          #     #          
         # #   # #         
            # #            
             #             
            # #            
         # #   # #         
          #     #          
         # #   # #         
# #   # #         # #   # #
 #     #           #     # 
# #   # #         # #   # #
   # #               # #   
    #                 #    
   # #               # #   
# #   # #         # #   # #
 #     #           #     # 
# #   # #         # #   # #
     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     
 

one answer from a coder:

link:https://www.nowcoder.com/practice/97fd3a67eff4455ea3f4d179d6467de9?tpId=40&tqId=21389&tPage=1&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

#include <stdio.h>
#include <math.h>

char d[3000][3000];
char s[6][6];
int n,m;

//递归画图
void dfs(int m,int x,int y) // m是扩张的维度,x是,y是基准坐标,第一个开始被分解的坐标
{
    int i,j;
    if(m==1)
    {
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                d[x+i][y+j]=s[i][j];
            }
        }
        return;
    }
    int size=pow(n,m-1); // 下一层的大小
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(s[i][j]!=' ')
                dfs(m-1,x+i*size,y+j*size); //对于不是空格的部分才进行扩展,是空格的部分不进行扩展
        }
    }
}

int main()
{
    int size;
    while(scanf("%d",&n)!=EOF)
    {
        getchar();
        if(n==0)
            break;
        int i,j;
        for(i=0;i<n;i++)
        {
            gets(s[i]);
        }
        scanf("%d",&m); // m是扩展的大小
        size=pow(n,m); // 整体的大小,行数和列数
        for(i=0;i<size;i++)
        {
            for(j=0;j<size;j++)
            {
                d[i][j]=' ';
            }
            d[i][size]='\0'; //注意,需要在末尾加结束符'\0',因为这样才能够正常输出字符数组
        }
        dfs(m,0,0);
        for(i=0;i<size;i++)
            printf("%s\n",d[i]);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值