Bingo!
Time limit1sMemory limit128 MB
Given column draw counts and X candidate 5x5 patterns, combine any Y of them into winning patterns and find the fewest additional draws that make some pattern fully markable.
- Level
Medium6 of 10
- Topics
- Brute force, Combinatorics, Implementation, Simulation
- Solved
- No attempts yet
Problem
Bingo is played on a grid, called a card. The five columns are labeled with the letters of the game's name: B, I, N, G, and O. Every square holds a number, and a player marks a square as soon as its number is drawn. A player wins a bingo the moment every square of some winning pattern on the card is marked.
The center square of the grid is a free space: it is already marked for every player from the start of the game.
The numbers drawn are the integers from to . Each block of fifteen consecutive numbers belongs to one column:
- B: to
- I: to
- N: to
- G: to
- O: to
You are told how many numbers have already been drawn in each column, together with the information that defines the winning patterns. In the most favorable case, determine the fewest additional numbers that still have to be drawn so that a bingo becomes possible.
Input
The first line contains a single integer , the number of data sets.
Each data set begins with a line of the form B I N G O X Y:
B,I,N,G,O— how many numbers have already been drawn in the corresponding column;X() — the number of input patterns;Y() — how many input patterns are combined to form a winning pattern.
The next lines describe the input patterns, printed side by side as grids. Inside a grid, X marks a square that must be covered and O marks a square that need not be covered.
A winning pattern is obtained by overlaying (taking the union of the marked squares of) any of the input patterns. The full set of winning patterns is every such combination, and marking all squares of any one of them yields a bingo.
For example, with , , and the input patterns
XXOOO OOOXX OOOOO OOOOO
XXOOO OOOXX OOOOO OOOOO
OOOOO OOOOO OOOOO OOOOO
OOOOO OOOOO XXOOO OOOXX
OOOOO OOOOO XXOOO OOOXX
the resulting winning patterns (marking any single one of them gives a bingo) are
XXOXX XXOOO XXOOO OOOXX OOOXX OOOOO
XXOXX XXOOO XXOOO OOOXX OOOXX OOOOO
OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO
OOOOO XXOOO OOOXX XXOOO OOOXX XXOXX
OOOOO XXOOO OOOXX XXOOO OOOXX XXOXX
Output
For each data set, print a single line containing the fewest additional numbers that still need to be drawn for a bingo to be possible.