Containing Zombies and Respecting Building Codes#3372

Containing Zombies and Respecting Building Codes

1 saniye
64 megabayt
Çok zor

It's well documented that the only way to thwart zombies is to trap them in closed fences. Zombie hunters have for years been luring zombies into clear spaces and then erecting such fences at lightning speeds so they can't go very far.

Unfortunately, the government has its zoning laws, its taxes, and its building codes, and recently they started to enforce silly constraints on how these zombie-enclosing fences can be constructed. According to city codes, fence posts must be planted at regular intervals, and the fences themselves must respect arbitrary limits on how many walls can surround a unit square of land. We also want to build as long an enclosing fence as possible, because the government has plenty of money to waste on such things.

Building Zombie Fences

Build walls between vertices to form a single enclosed fence without crossings or branches. The number indicates exactly how many walls—according to the city building codes—must surround that square (and a lack of number means there's no constraint). So, presented with the following 5 \times 5 grid of land squares:

the following fence could be constructed:

The grid of lots is always n \times n (1 \leq n \leq 6), and each lot is either a number (, , , or ) to impose a constraint, or a blank if no constraint is being imposed. You are to output the length of the longest possible loop (or equivalently, the number of vertices in the loop), or if no loop exists. Note that loops of length are invalid. A valid loop must enclose a nonzero amount of area.

Giriş

The input consists of a series of zombie fencing problems, expressed through , the dimension of the problem, followed by an n \times n grid (1 \leq n \leq 6) with the number constraints ( to represent no constraint), followed by a blank line. The end of input is marked by a single .

Çıktı

For each fence problem, you should print the length of the longest fence loop that can be constructed for that problem while still respecting all constraints, or print if the problem has no such solution. There should be no extraneous whitespace, except for the newlines that separate each of the fence lengths.

Örnekler

Örnek 1

Loading
Loading