Commedia dell'arte
Time limit1sMemory limit128 MB
Determine whether a 3D M^3 sliding puzzle can be restored to its solved state given its parity/permutation solvability rules.
- Level
Medium6 of 10
- Topics
- Math, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
Commedia dell'arte is a theater genre that first appeared in Italy at the start of the 16th century, inspired by Roman theater. The plays had no fixed script, so the actors (also called performers) had to improvise constantly — the author gave only loose directions such as "enter the stage and do something funny" or "everyone comes on stage and everything ends happily."
A theater company wants to stage a brand-new play. Its hero owns a puzzle that plays a central role and opens up many chances to improvise. The puzzle is the world-famous Fifteen Puzzle, but to make the play more interesting the company replaces the standard puzzle with a three-dimensional version.
The 3D puzzle is a cube of slots. Every slot but one holds a cubic tile; exactly one position is free. The tiles are numbered from to . The goal is to restore the original ordering after the tiles have been randomly reshuffled. The only allowed move is to slide a tile that is adjacent to the free position into that free position, along one of the three principal axes.
In the original (goal) configuration, the slot at coordinates with holds tile number , and the slot is free.
Write a program that decides whether the puzzle can be solved.
Input
The input consists of cases. The first line contains a single positive integer . The cases follow.
The first line of each case contains one integer (), the side length of the cube. Then follow lines, each containing exactly numbers that describe one layer: the first line is the top layer of the cube and the last line is the bottom layer. Within a layer the numbers are listed row by row, from the top-left corner to the bottom-right corner. Equivalently, the slot at coordinates is the -th number on the -th line. Numbers are separated by spaces, and marks the free position.
Output
For each case print exactly one line. If the original configuration can be reached by sliding the tiles, print Puzzle can be solved. Otherwise print Puzzle is unsolvable.