Jengaism
Time limit1sMemory limit128 MB
Simulate Jenga moves (remove a block, place it on top) and report when any structure topples because its center of gravity leaves the convex hull of its supports.
- Level
Hard8 of 10
- Topics
- Geometry, Simulation, Implementation, Greedy
- Solved
- No attempts yet
Problem
Jenga is a popular game in which a tower is built from blocks. Initially the tower has levels, each made of three blocks laid side by side. Blocks on adjacent levels are oriented at right angles to one another, so every block touches all three of the blocks in the level directly above it and directly below it. A picture of the real game is shown below.

On each turn a player removes one block from somewhere in the tower and places it on top. The goal is to keep doing this without knocking the tower over. A block is always removed from below the highest completed level, and the top level is always completed (at right angles, of course) before a new level is started.
Write a program that reads the moves of a Jenga game in order and determines the moment at which the tower — or any part of it — falls or topples.
A structure topples if the vertical projection of its center of gravity onto its base lies outside the convex hull of its support points. If the center of gravity lies exactly on the edge of that hull, the structure is considered stable.
Input
The first line contains , the number of moves. Each of the next lines describes one move as 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 placed. A location is written as a number giving the level, followed by a letter A–C giving the position within that level (left to right, or front to back). For example, in the initial tower the top level consists of the blocks 18A, 18B, and 18C. The diagrams below label the pieces as seen from the front and from the right side.
Output
If the tower collapses after removing a block at location , print The tower collapses after removing L.
If the tower collapses after placing a block at location , print The tower collapses after placing L.
If every move executes successfully without the tower falling, print The tower never collapses.