Decide whether a tree-like polycube of eight cubes folds along shared faces into the surface of a four-dimensional hypercube.
Hard8BacktrackingGeometryNo attempts yetTime limit1sMemory limit256 MBA 4-hypercube is also called a tesseract. A unit solid tesseract is the convex hull of the 16 points with coordinates (±1/2,±1/2,±1/2,±1/2), and those 16 points are its vertices. It has 32 edges, 24 square faces, and 8 cubical 3-faces, which are called cells. This problem studies hollow tesseracts, so a tesseract means the boundary of a solid tesseract. A tesseract is therefore a connected union of 8 solid cubes, its cells, that meet each other at 24 square faces, 32 edges, and 16 vertices.
Cut a tesseract along 17 of its 24 square faces so that it stays connected through the 7 faces left intact. Then unfold it into a 3D hyperplane by rotating its cells around the faces left intact until every cell lies in the same hyperplane. The result is a 3-net of the tesseract. This is the natural generalization of cutting a cube along 7 of its 12 edges and unfolding it onto a plane to get a 2-net made of 6 squares.
You are given a tree-like 8-polycube in 3D space, also called an octocube. An octocube is a collection of 8 unit cubical cells joined face to face. More formally, the intersection of each pair of its cells is empty, a point, a unit segment, or a unit square. Two cells are adjacent when their intersection is a square. Cells that intersect at a point or a segment are not adjacent. The adjacency graph of an octocube has 8 vertices, one per cell, and an edge for every adjacent pair of cells. An octocube is tree-like when its adjacency graph is a tree.
Determine whether the given tree-like octocube is a 3-net of a tesseract, that is, whether it can be folded in 4D space along the squares shared by its cells into a tesseract.
For example, look at the leftmost picture below. It shows a wire frame of a tree-like octocube. Rotate cell GHLKG1H1L1K1 around plane GHLK and cell FGKJF2G2K2J2 around plane FGKJ by 90 degrees into the fourth dimension, outside of the original hyperplane. Point G1 then joins G2 and point K1 joins K2, and face GKK2G2 is glued to face GKK1G1. The result is shown on the right. The fourth dimension is projected orthographically onto the three shown in perspective. The points that moved out of the original hyperplane are marked with hollow dots.

Rotate EFJIE1F1J1I1 around EFJI and EHLIE2H2L2I2 around EHLI. The result is shown on the left of the next picture. The remaining steps are as follows. Rotate MNOPQRST around MNOP, then rotate both MNOPQRST and IJKLMNOP around IJKL, then rotate ABCDEFGH around EFGH. The last step is to glue every pair of faces that meet, which gives the tesseract shown on the right.

The first line contains three integers m, n, k, the width, the depth, and the height of a box that contains the given octocube (1≤m,n,k≤8). The following k groups of lines describe rectangular slices of the box from top to bottom. Each slice is described by n rows of m characters each. Every character is either '.', denoting empty space, or 'x', denoting a unit cube. The box contains the octocube but need not fit it tightly. The input is guaranteed to describe a tree-like octocube, so exactly 8 characters are 'x'.
Print a single word: "Yes" if the given octocube can be folded into a tesseract, or "No" otherwise.