Rings and Runes
Time limit1sMemory limit128 MB
Validate the runes for several gates, report the highest-priority error, then decide if the resulting 3-CNF formula is satisfiable.
- Level
Medium7 of 10
- Topics
- Simulation, Implementation, Backtracking, Bit manipulation
- Solved
- No attempts yet
Problem
Frodo has entered the mines of Moria and reached a series of gates. Each gate bears an ancient riddle describing the state of a set of special control rings. From the riddle Frodo must decide whether the gate can be opened or whether it is simply a death trap.
A riddle is made of several runes. A valid rune contains exactly 3 statements about 3 different rings. Each statement is true or false depending on whether a specific ring is spinning or still. A riddle need not mention every ring in the gate's controlling set.
To open a gate the hobbits choose which rings to spin and which to leave still, then speak an incantation. The gate opens only if the whole riddle is satisfied, meaning every rune has at least one true statement.
Notation: a statement is written as a signed ring number. A positive number is true when ring is spinning; a negative number is true when ring is not spinning. For example, the rune 1 -2 3 0 is true when (ring 1 is spinning) OR (ring 2 is not spinning) OR (ring 3 is spinning). The trailing 0 marks the end of the rune. A ring may appear at most once within a single rune, but the same ring may be reused across different runes.
Input
- The first line contains an integer (): the number of gates.
- For each gate, the first line contains two integers
rings() andrunes(), separated by a space. Rings are numbered throughrings; a riddle need not use every ring. - The next
runeslines each describe one rune as four space-separated integers . The three statements are (each a signed 32-bit integer); the trailing terminates the rune.
Output
For each gate, output exactly one line. If any rune contains an error, report only the single highest-priority error, using this priority order:
- If any rune contains a null ring (a statement equal to or ), the whole riddle is invalid. Output
INVALID: NULL RING. - Otherwise, if any rune contains a statement with or , output
INVALID: RING MISSING. (Do not report this if the riddle already has a null ring.) - Otherwise, if any single rune refers to the same ring more than once (for example
-2 2 3 0or3 1 1 0), outputINVALID: RUNE CONTAINS A REPEATED RING. - Otherwise the riddle is well-formed. Identical repeated runes count as one. If some choice of spinning and still rings satisfies every rune, output
RUNES SATISFIED!. If no choice can satisfy every rune, outputRUNES UNSATISFIABLE! TRY ANOTHER GATE!.