Repeater

POJ - 3768

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

# #   # #

 #     #

# #   # #

   # #   

    #    

   # #   

# #   # #

 #     # 

# #   # #

Input

The input contains multiple test cases.
The first line of each case is an integer N, representing the size of the template is NN (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.

Output

For each test case, just print the Level Q picture by using the given template.

Sample Input

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

Sample 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     

思路:

分形的经典问题,

将大图形转为很多个小图形处理,小图形也继续分为更小的图形处理。

用dfs递归操作即可,要维护好当前图形的左上角的坐标和当前步骤的图形尺寸(边长),以及当前是第几层,到第一层的时候直接赋值为单元元素。

个人觉得自己写法属于比较简单的。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}

inline void getInt(int* p);
const int maxn = 3000 + 10;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/

bool s[maxn][maxn];
char a[maxn][maxn];
int x;
void dfs(int lx, int ly, int rx, int ry, int len, int st)
{
    // cout << lx << " " << ly << " " << rx << " " << ry << " " << len << " " << st << endl;
    if (len == 1)
    {
        s[lx][ly] = 1;
        return ;
    }
    int to = len / x;
    repd(i, 1, x)
    {
        rep(j, 0, x)
        {
            if (a[i][j] != ' ')
            {
                dfs(lx + (i - 1)*to, ly + (j)*to, lx + (i - 1)*to + to - 1, ly + (j)*to + to - 1, to, st - 1);
            }
        }
    }

}
char base;
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    while (~scanf("%d", &x))
    {
        if (!x)
        {
            break;
        }
        // cin >> base;
        getchar();
        repd(i, 1, x)
        {
            gets(a[i]);
            // getline(cin, a[i]);
        }
        repd(i, 1, x)
        {
            rep(j, 0, x)
            {
                if (a[i][j] != ' ')
                {
                    base = a[i][j];
                }
//                cout<<a[i][j];
            }
//            cout<<endl;
        }
        // chu(base);
        int step;
        // cin >> step;
        scanf("%d", &step);
        int len = (int)(pow(x, step) + 0.5);
        dfs(1, 1, len, len, len, step);
        repd(i, 1, len)
        {
            repd(j, 1, len)
            {
                if (s[i][j])
                {
                    putchar(base);
                    // cout << base;
                } else
                {
                    putchar(' ');
                    // cout << ' ';
                }
                s[i][j] = 0;
            }
            putchar('\n');
            // cout << endl;
        }
    }

    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}