Mine Layer (Small)
Time limit5sMemory limit512 MB
Given a small Minesweeper-style clue grid (R is 3 or 5, C is 3 to 5), find the maximum number of mines the middle row can hold over all layouts that match the clues.
- Level
Medium6 of 10
- Topics
- Brute force, Backtracking, Implementation, Array
- Solved
- No attempts yet
Problem
MineLayer is a Minesweeper style puzzle played on an by grid. Each square of the grid holds one mine or no mine at all. A MineLayer puzzle is a grid of numbers, and each number is the total number of mines in every square adjacent to that square plus the square itself. A number is therefore between 0 and 9.
The goal is to find a mine layout that matches the given numbers.
Below is a 3 by 4 grid. The original layout is on the left and the puzzle is on the right.

One grid of numbers can match several layouts, so find the largest number of mines the middle row can hold. The number of rows is always odd, so the middle row is row from the top. Every puzzle has at least one solution.
Input
The first line contains the number of test cases . test cases follow.
The first line of each test case contains two space separated numbers: , the number of rows, and , the number of columns. is always odd. Each of the next lines contains the numbers of that row, separated by spaces.
Limits
- Every puzzle has at least one solution.
- or
Output
For each test case, print one line containing "Case #X: Y", where is the 1 based test case number and is the largest number of mines the middle row holds among all layouts that satisfy the given numbers.