Dragon Curve
Time limit1sMemory limit512 MB
Generate the segments of N dragon curves on a 101 by 101 grid, mark the grid points they pass through, and count unit squares whose four corners are all marked.
- Level
Medium6 of 10
- Topics
- Implementation, Recursion, Simulation, Math
- Solved
- No attempts yet
Problem
A dragon curve is fixed by three values and it is defined on the two dimensional coordinate plane. On that plane the x axis runs in the → direction and the y axis runs in the ↓ direction.
- Starting point
- Starting direction
- Generation
A generation 0 dragon curve is a single segment of length 1. The figure below is the generation 0 dragon curve that starts at with the starting direction to the right.

The generation 1 dragon curve is the generation 0 curve turned 90 degrees clockwise about its end point and then attached to the end point of the generation 0 curve. The end point is the point that lies farthest away when you travel from the starting point along the segments.

The generation 2 dragon curve is built from generation 1 by the same method. The blue segments are the ones just attached.

The generation 3 dragon curve is built from the generation 2 curve. The figure below is the generation 3 dragon curve.

In other words, the generation dragon curve () is the generation curve turned 90 degrees clockwise about its end point and attached to that end point.
A grid of size holds dragon curves. Write a program that counts the squares of size whose four corners all belong to a dragon curve. A grid coordinate is written , and only the coordinates with and are valid.
Input
The first line has the number of dragon curves (). Each of the next lines describes one dragon curve with four integers , , , . Here and are the starting point, is the starting direction, and is the generation. (, , )
No dragon curve given in the input leaves the grid. Dragon curves can overlap each other.
The direction is one of 0, 1, 2, 3 and means the following.
- 0: the direction in which the x coordinate increases (→)
- 1: the direction in which the y coordinate decreases (↑)
- 2: the direction in which the x coordinate decreases (←)
- 3: the direction in which the y coordinate increases (↓)
Output
Print on the first line the number of squares of size whose four corners all belong to a dragon curve.

