CCO '04 P5 - Jengaism

View as PDF

Submit solution

Points: 7
Time limit: 1.0s
Memory limit: 32M

Problem type
Canadian Computing Competition: 2004 Stage 2, Day 2, Problem 2

Jenga is a popular game involving a tower constructed of 1 \times 1 \times 3 blocks. Initially, this tower has 18 levels, each consisting of three blocks laid side to side. The blocks on alternating levels are oriented at right angles. Thus, each block touches all three of the blocks in the levels above and below it. Here is a picture of the actual game:

Play involves each player removing a block from somewhere in the tower, and putting it in a new position on top. The goal is to do this without knocking over the tower. Blocks are always removed from below the highest complete level, and the top level is always completed before a new level is started (at right angles, of course).

Write a program which reads sequential moves of a Jenga game and determines at what point the tower (or any part of it) falls or topples.

Note that a structure will topple if its center of gravity, projected onto its base, lies outside the convex hull of its support points. If the center of gravity lies exactly on the edge of this hull, we will assume that the structure is stable.

Input Specification

The first line contains N, the number of moves. The next N lines describe one move per line, with two locations separated by a single space. The first is the location of the block to be removed, and the second is where it will be put back. A location is specified as a number, specifying the level, and then a letter A-C, specifying its position in the level (left to right or front to back). For instance, the top level of the initial tower configuration consists of blocks at 18A, 18B, and 18C. Below is a diagram of the pieces, labelled with a front and right side perspective.

Front
18C
17A17B17C
16C
15A15B15C
14C
13A13B13C
12C
11A11B11C
10C
9A9B9C
8C
7A7B7C
6C
5A5B5C
4C
3A3B3C
2C
1A1B1C
Right Side
18A18B18C
17C
16A16B16C
15C
14A14B14C
13C
12A12B12C
11C
10A10B10C
9C
8A8B8C
7C
6A6B6C
5C
4A4B4C
3C
2A2B2C
1C

Output Specification

If the tower collapses after removing a block at location L, output The tower collapses after removing L.

If the tower collapses after placing a block at location L, output The tower collapses after placing L.

If all moves execute successfully (i.e., without causing the tower to fall), output The tower never collapses.

Sample Input 1

4
6B 19B
7B 19A
17B 19C
17A 20B

Sample Output 1

The tower collapses after removing 17A

Sample Input 2

2
17C 19C
17A 19A

Sample Output 2

The tower never collapses

Comments

There are no comments at the moment.