This page is still under construction.

Parts of this page are still being built. What you see may change.

Manelzuma's Revenge

Time limit1sMemory limit128 MB

Summary
Given a recursive square-substitution rule, answer queries that print a rectangular window of one generated fractal iteration.
Level

Hard8 of 10

Topics
Recursion, Divide and conquer
Solved
No attempts yet

Problem

Recall the classic fractal that starts with a filled square, divides it into 99 sub-squares, and clears the middle one:

* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * *       * * *
* * *       * * *
* * *       * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *

This operation is then repeated for each of the 88 remaining sub-squares, and so on. Choosing a different operation gives a different fractal. For example, we could clear the upper-left and lower-right corners instead of the middle, or divide the square into a different number of sub-squares.

Here the fractal operation is given as an n×nn \times n grid of characters, where each character performs a particular substitution on the corresponding sub-square:

  • . (period): the sub-square becomes completely empty.
  • ! (exclamation mark): the sub-square becomes full and is not subject to further operations.
  • ? (question mark): the sub-square stays full and is subject to further operations.

The fractal above is represented by the 3×33 \times 3 grid:

???
?.?
???

Four iterations of the fractal denoted by the 2×22 \times 2 grid

.!
?.

look like this (iteration 11 through iteration 44):

* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *

        * * * *
        * * * *
        * * * *
        * * * *
* * * *        
* * * *        
* * * *        
* * * *        

        * * * *
        * * * *
        * * * *
        * * * *
    * *        
    * *        
* *            
* *            

        * * * *
        * * * *
        * * * *
        * * * *
    * *        
    * *        
  *            
*              

Your task is to print a region of an arbitrary fractal specified by an n×nn \times n grid. These fractals grow far too large to store completely in memory, so you must compute only the requested region.

Input

The first line contains a positive integer nn (1≤n≤1001 \le n \le 100). The next nn lines each contain nn characters drawn from ., !, ?, defining the fractal operation to be iterated.

The rest of the input is a sequence of region queries. Each query is given on two lines:

  • The first line contains kk, the number of iterations applied to the object. Iteration 00 is a completely filled square of size nk×nkn^k \times n^k, so after kk iterations the whole object is nk×nkn^k \times n^k.
  • The second line contains four integers bb, tt, ll, rr: the bottom row, top row, left column, and right column of the region to print.

Rows are numbered from bottom to top starting at 11, and columns from left to right starting at 11. The input ends with a line containing -1 in place of a kk value.

Constraints: k≤100k \le 100; each of bb, tt, ll, rr is at most one million; and the width and height of the region are each at most 100100.

Output

For each query, print the requested region, one line per row, with the top row first and the bottom row last. Print * for a filled cell and a space for an empty cell, and separate adjacent cells in a row with a single space so the output looks roughly square. Print a blank line after each region.

Examples3

  1. Example 1

    Input
    2
    .!
    ?.
    3
    2 8 1 7
    -1
    
    Expected output
            * * *
            * * *
            * * *
            * * *
        * *      
        * *      
      *          
    
  2. Example 2

    Input
    3
    ???
    ?.?
    ???
    2
    1 9 1 9
    -1
    
    Expected output
    * * * * * * * * *
    *   * *   * *   *
    * * * * * * * * *
    * * *       * * *
    *   *       *   *
    * * *       * * *
    * * * * * * * * *
    *   * *   * *   *
    * * * * * * * * *
    
  3. Example 3

    Input
    2
    .!
    ?.
    2
    1 4 1 4
    3
    1 4 1 4
    -1
    
    Expected output
        * *
        * *
      *    
    *      
    
        * *
        * *
      *    
    *