Mine Layer (Large)
Time limit5sMemory limit512 MB
Given a Minesweeper-style grid of neighbor counts, find the maximum number of mines the middle row can hold in any layout that matches all counts.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Implementation, Brute force
- Solved
- No attempts yet
Problem
Mine Layer is a puzzle much like Minesweeper. The board is an grid, and every square either holds one mine or holds none.
The puzzle is given as a grid of numbers. Each number counts the mines in that square itself together with the squares next to it in all eight directions. Squares outside the board are not counted, so the numbers run from 0 to 9.
The goal is to find a mine layout that matches every given number.
The picture below shows a grid. The original layout is on the left, and the puzzle built from it is on the right.

A puzzle can have several layouts, so print the largest number of mines the middle row can hold. The number of rows is always odd, so the middle row is row counted from the top, and at least one layout always matches the numbers.
Input
The first line contains the number of test cases . test cases follow.
The first line of each test case contains the number of rows and the number of columns , separated by a space. is always odd. Each of the next lines contains the numbers of that row, separated by spaces.
Limits
- Every puzzle has at least one matching layout.
- is an odd number between 3 and 49, inclusive.
Output
For each test case, print one line in the form Case #X: Y, where is the 1-based test case number and is the largest number of mines the middle row can hold in a layout that matches every given number.