Dots and Boxes
Time limit2sMemory limit512 MB
Given a Dots and Boxes position with no completed square, find the longest sequence of moves that still avoids closing any square, then print that length plus one.
- Level
Hard8 of 10
- Topics
- Graph, Dynamic programming, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
Alice and Bob play Dots and Boxes on a square lattice of dots. They move alternately. One move draws a segment between two dots that are neighbours horizontally or vertically and are not connected yet. When four segments close a unit square, the player who drew the last of those four segments scores one point for that square. The game ends once every possible segment has been drawn, and the player with more points wins.
Today the two are not in a competitive mood, so neither follows a strategy. Even when a move would score a point, a player can leave it and play somewhere else. They have been playing for a while and neither has scored yet. If nobody scores soon, they will get bored.
You are given the current position. Let be the largest number of moves that can still be played without closing a single square. After those moves every remaining segment closes a square, so the move numbered scores a point. Print , the number of moves that can be made in the worst case before Alice or Bob has certainly scored.
Input
The first line contains one integer (), the number of dots along one side of the lattice.
The next lines contain characters each and describe the current position in row major order. Rows and columns are numbered from .
- Cell is
*and marks dot , for and . - Cell is
-when dots and are connected by a segment, and.otherwise, for and . - Cell is
|when dots and are connected by a segment, and.otherwise, for and . - Cell is
., for and .
No unit square is closed in the given position, so neither player has scored.
Output
Print one integer, the number of moves that can be made in the worst case before Alice or Bob has certainly scored a point.