Nowadays there are sandwiches of almost any ingredient. A crazy chef has just discovered recursion, so he is considering making sandwiches of sandwiches (of sandwiches ...). Since thinking in three dimensions is somehow difficult, he first wants to study the following two-dimensional model for making rectangular sandwiches.
Square pieces, with height 1 and length 1, correspond to ingredients; rectangular pieces, with height 1 and length (for any natural number ), correspond to slices of bread. Then, a recursive sandwich (RS) is defined by these rules:
Any single ingredient is a RS.
Let , , and let be any sequence of RS’s where , and such that for every , and . Then
The result of placing a slice at the bottom of is a RS.
The result of placing a slice at the top of and another slice at the bottom of is a RS.
There are no RSs other than those constructable from rules 1, 2(a) and 2(b).
These are some examples of recursive sandwiches:
And these are some examples of wrong sandwiches:
Write a program to tell if some given sandwiches follow the rules of the crazy chef or not.
Input consists of several cases. Every case begins with the name of the sandwich, followed by its height , its length , and its number of pieces . Then follow triples of integer numbers , and describing every piece: is the position of the left square of the piece; is the length of the piece. Assume and . The lower-left position of a sandwich is . The sum of the areas of the pieces is exactly , and every position in is covered by a piece.
(The sandwich aaa in the sample input is
the second of the recursive sandwiches given as example. The sandwich
bbb is the fourth of the wrong
sandwiches.)
For every given sandwich, tell if it is a recursive sandwich or not.
Input
aaa 2 2 3 2 1 1 2 2 1 1 1 2 bbb 3 1 3 1 1 1 2 1 1 3 1 1
Output
aaa is a recursive sandwich bbb is NOT a recursive sandwich