The Sidewinder Sleeps Tonite
Time limit1sMemory limit128 MB
Given a drawn grid of segments and cell numbers, decide whether the drawing is a single closed loop matching every numbered cell.
- Level
Medium5 of 10
- Topics
- Graph, DFS, Simulation, Implementation
- Solved
- No attempts yet
Problem
Slitherlink is a logic puzzle popularized by the Japanese puzzle publisher Nikoli, the same company that made Sudoku famous around the world. It has a very small set of rules that can nonetheless produce devilishly difficult (and delightful) puzzles.
The rules of Slitherlink are as follows:
- The board is a lattice of dots. In this problem the lattice is always a regular rectangular grid.
- Some of the cells formed by the lattice contain a number. On a rectangular lattice each such number is between 0 and 3 inclusive.
- You draw line segments that connect horizontally or vertically adjacent dots (the sides of the cells). A correct solution forms a single closed loop that never crosses or touches itself, with no segments that are not part of the loop (no dangling segments and no additional separate loops). In addition, every numbered cell must have exactly that many of its four sides used by the loop.
You are given a Slitherlink board that is claimed to be solved. Determine whether it is indeed a legitimate solution.
Input
The first line contains a single integer N (), the number of data sets. Each data set is given as follows:
- A line with two integers H and W (): the height and width of the puzzle measured in cells (not dots).
- Then lines describing the board, using only the following non-whitespace characters:
0,1,2,3,?: the number written inside a cell;?marks a cell with no number.#: a dot of the lattice.-,|: a horizontal or vertical line segment between two adjacent dots..: an absent connection between two adjacent dots.
Every board is written out in full: a line never contains internal whitespace to stand for an empty cell or a missing connection, so each of the lines has exactly characters.
Output
For each data set, print VALID if the board is a correct solution of the given Slitherlink, or INVALID otherwise. Print each answer on its own line.