Manelzuma's Revenge
Time limit1sMemory limit128 MB
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 sub-squares, and clears the middle one:
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
This operation is then repeated for each of the 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 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 grid:
???
?.?
???
Four iterations of the fractal denoted by the grid
.!
?.
look like this (iteration through iteration ):
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* *
* *
* *
* *
* * * *
* * * *
* * * *
* * * *
* *
* *
*
*
Your task is to print a region of an arbitrary fractal specified by an 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 (). The next lines each contain 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 , the number of iterations applied to the object. Iteration is a completely filled square of size , so after iterations the whole object is .
- The second line contains four integers , , , : the bottom row, top row, left column, and right column of the region to print.
Rows are numbered from bottom to top starting at , and columns from left to right starting at . The input ends with a line containing -1 in place of a value.
Constraints: ; each of , , , is at most one million; and the width and height of the region are each at most .
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.