Fold-up Patterns
Time limit1sMemory limit128 MB
Given a planar net of unit squares with specified fold directions on shared edges, determine whether folding yields a closed surface of a solid and report its volume.
- Level
Hard8 of 10
- Topics
- Geometry, Graph, DFS, Simulation
- Solved
- No attempts yet
Problem
Fold-up patterns for solids such as cubes or octahedrons appear in many geometry books, but without actually folding one it is hard to tell whether the construction really works. In this problem we study a special class of such patterns.

You are given a fold-up pattern made of unit squares in the plane, together with a description of which edges to fold and in which direction. Decide whether folding it produces the closed surface of a three-dimensional solid, and if it does, find the volume of that solid.
More precisely, the pattern is a connected set of unit squares in the plane. For every edge that joins two connected squares you are told whether to fold forward, fold backward (always by a right angle), or not fold at all along that edge. If an edge between two adjacent squares is not listed in the input, the squares are not connected there and may be pulled apart while folding. Connected edges must always be folded exactly as described.
For our purposes a closed surface is one in which every square separates the inside from the outside. After folding, the squares lie on a three-dimensional unit grid, and each square separates one cell (a cube of side length one) on the inside from one cell on the outside. For every cell it must be clear whether it is inside or outside.

Note that even a pattern whose interior is not connected can still be a closed surface.
Two distinct squares may not occupy exactly the same position in space, although they may (and, for a closed surface, will) touch along edges and at vertices. Make sure the pattern does not pass through itself along connected edges. Apart from that, do not worry about the physical folding process, for example which edges are folded first or whether part of the structure is in the way of the rest.
Input
The input consists of several scenarios.
Each scenario begins with a line containing two integers and : the number of squares () and the number of edges (). Squares are labelled to . Each of the next lines describes one edge with four integers :
- and (with ): the two squares joined by the edge.
- : the position of square relative to square . means is above, to the left, below, or to the right of , respectively.
- : how to fold along the edge. means do not fold, fold forward, or fold backward, respectively.

You may assume the pattern is connected and can be drawn in the plane without overlap.
The input ends with a line containing two zeros in place of and . Do not process that line.
Output
For each scenario print Test case #k:, where k is the scenario number (starting from 1). On the same line print either not a closed surface if the pattern does not form a closed surface, or closed surface, volume=V, where V is the volume as an integer, if it does.
Notes
