Bingo!

Time limit1sMemory limit128 MB

Summary
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 5×55 \times 5 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 11 to 7575. Each block of fifteen consecutive numbers belongs to one column:

  • B: 11 to 1515
  • I: 1616 to 3030
  • N: 3131 to 4545
  • G: 4646 to 6060
  • O: 6161 to 7575

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 nn, 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 (1≤X≤191 \le X \le 19) — the number of input patterns;
  • Y (1≤Y≤min⁡(5,X)1 \le Y \le \min(5, X)) — how many input patterns are combined to form a winning pattern.

The next 55 lines describe the XX input patterns, printed side by side as 5×55 \times 5 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 YY 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 X=4X = 4, Y=2Y = 2, 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.

Examples1

  1. Example 1

    Input
    3
    0 1 0 2 1 4 2
    XXOOO OOOXX OOOOO OOOOO
    XXOOO OOOXX OOOOO OOOOO
    OOOOO OOOOO OOOOO OOOOO
    OOOOO OOOOO XXOOO OOOXX
    OOOOO OOOOO XXOOO OOOXX
    1 1 0 1 1 5 1
    XXXXX OOOOO OOOOO OOOOO OOOOO
    OOOOO XXXXX OOOOO OOOOO OOOOO
    OOOOO OOOOO XXXXX OOOOO OOOOO
    OOOOO OOOOO OOOOO XXXXX OOOOO
    OOOOO OOOOO OOOOO OOOOO XXXXX
    15 15 15 15 4 1 1
    XXXXX
    XXXXX
    XXXXX
    XXXXX
    XXXXX
    
    Expected output
    4
    0
    1