Pablo Squarson's Headache
InterviewTime limit1sMemory limit128 MB
Given how N unit squares are attached one by one to an existing square in one of four directions, compute the width and height of the resulting figure.
- Level
Medium4 of 10
- Topics
- Simulation, Array, Hash map, Implementation
- Solved
- No attempts yet
Problem
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares".
In the center of his studio there is a huge table, and beside it are many squares, all of the same size. Pablo first places one square on the table. Then he places the remaining squares one by one, always attaching each new square flush against one side of a square that is already on the table, with machine-like precision. No square is ever placed on top of another.
When a piece is finished, Pablo wants to know the size of the box that would fit it, that is, the width and the height of the artwork. Given a record of how Pablo built a piece, write a program that computes its width and height. The side length of each square is 1.
Input
The input consists of several datasets. Each dataset describes how Pablo built one piece of artwork and has the following format.
N
n1 d1
n2 d2
...
nN-1 dN-1
The first line contains the number of squares used to make the piece. is a positive integer smaller than .
The remaining lines are square placement instructions. The squares are numbered in the order they are placed: the first square is numbered , and the squares placed after it are numbered . No placement instruction is given for square .
The -th instruction "" means that square () is placed next to square in the direction , where is for left, for down, for right, and for up.
The end of the input is indicated by a line containing a single zero.
Output
For each dataset, output one line containing the width and the height of the piece of artwork, separated by a single space. Each line must not contain any other characters.