This page is still under construction.

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

Plato's Blocks

Time limit1sMemory limit128 MB

Summary
Given three n by n shadow patterns, decide whether one connected solid built from unit cubes can cast all three shadows simultaneously.
Level

Medium6 of 10

Topics
Backtracking, Brute force, Implementation, Matrix
Solved
No attempts yet

Problem

Plato believed that what we perceive is only a shadow of reality. Recent archaeological excavations suggest this belief may have grown out of Plato's youthful fascination with a set of cleverly designed blocks. Each block has the curious property that, when it is held with any one face toward a light source, it casts the shadow of some letter, number, shape, or pattern. The three faces meeting at a single corner can correspond to three different shadow patterns; opposite faces, naturally, cast shadows that are mirror images of one another.

Each block is built by gluing small unit cubes together into a single connected object. As an example, the figures below show, layer by layer, the internal structure of a block that can cast the shadows of the letters "E", "G", or "B".

Only part of the original set of blocks has been recovered, but curious scientists want to know which combinations of shadows are possible. Your program will help them: it reads groups of three shadow patterns and, for each group, reports whether a single solid block can be built that casts exactly those three shadows.

Input

The input contains a sequence of data sets. Each data set specifies a dimension followed by three shadow patterns. The first line of a data set contains a positive integer nn (1≤n≤201 \le n \le 20) giving the size of the patterns. The rest of the data set consists of 3n3n lines, each a string of nn characters drawn from "X" and "-". Every group of nn consecutive lines is one pattern. An "X" marks a position where the finished solid must cast a shadow, and a "-" marks a position where light must pass through. You may assume that each input pattern has at least one "X" on every edge. The input ends with a line containing a single 0 in place of a dimension.

Output

For each data set, print the data set number together with one of the following two messages:

Valid set of patterns
Impossible combination

For the ii-th data set, print the answer on a single line in the form Data set i: Valid set of patterns or Data set i: Impossible combination (the number ii starts from 1). A set of patterns is valid if it is possible to build, by gluing unit cubes together face to face, a single connected solid that casts the shadow of each of the three input patterns.

Examples2

  1. Example 1

    Input
    5
    XXXXX
    X----
    X--XX
    X---X
    XXXXX
    XXXXX
    X----
    XXXXX
    X----
    XXXXX
    XXXXX
    X---X
    XXXX-
    X---X
    XXXXX
    3
    X--
    -X-
    --X
    XX-
    XXX
    -XX
    -XX
    XXX
    XX-
    0
    
    Expected output
    Data set 1: Valid set of patterns
    Data set 2: Impossible combination
    
  2. Example 2

    Input
    1
    X
    X
    X
    0
    
    Expected output
    Data set 1: Valid set of patterns