This page is still under construction.

Parts of this page are still being built. What you see may change.

Fold-up Patterns

Time limit1sMemory limit128 MB

Summary
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 nn and ee: the number of squares nn (1≤n≤2001 \le n \le 200) and the number of edges ee (0≤e≤3000 \le e \le 300). Squares are labelled 00 to n−1n-1. Each of the next ee lines describes one edge with four integers s1 s2 p fs_1\ s_2\ p\ f:

  • s1s_1 and s2s_2 (with 0≤s1<s2<n0 \le s_1 < s_2 < n): the two squares joined by the edge.
  • pp: the position of square s2s_2 relative to square s1s_1. p=0,1,2,3p = 0, 1, 2, 3 means s2s_2 is above, to the left, below, or to the right of s1s_1, respectively.
  • ff: how to fold along the edge. f=0,1,2f = 0, 1, 2 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 nn and ee. 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

Examples3

  1. Example 1

    Input
    6 5
    0 2 2 1
    1 2 3 1
    2 3 3 1
    2 4 2 1
    4 5 2 1
    5 4
    0 2 2 1
    1 2 3 1
    2 3 3 1
    2 4 2 1
    0 0
    
    Expected output
    Test case #1: closed surface, volume=1
    Test case #2: not a closed surface
    
  2. Example 2

    Input
    6 5
    0 2 3 2
    0 3 1 2
    0 4 0 2
    0 5 2 2
    1 2 1 2
    0 0
    
    Expected output
    Test case #1: closed surface, volume=1
    
  3. Example 3

    Input
    10 9
    0 1 3 2
    0 2 1 2
    0 3 0 2
    0 4 2 2
    1 6 3 0
    2 7 1 0
    3 8 0 0
    4 9 2 0
    5 6 1 2
    0 0
    
    Expected output
    Test case #1: closed surface, volume=2