Problem p00626 - Generation 3

Orig Description

Problem F: Chocolate with Heart Marks
Taro loves chocolate, and after returning from school, he eats his favorite chocolate bar with heart marks. His recent enjoyment is to eat all the blocks without heart marks while leaving all the heart-shaped blocks until the end. Taro tries to eat as many blocks without heart marks as possible while keeping all the blocks with heart marks connected.
However, Taro is still young, and even if he tries to eat as described above, some blocks will be left uneaten. Therefore, Taro has asked you to create a program that finds the maximum number of blocks that can be eaten when all the blocks with heart marks remain connected.
Taro can eat the chocolate bar in any way as long as all blocks with heart marks are connected. If any side of a block is not touching another block, it is considered separated.
Input
The input consists of multiple datasets. Each dataset is given in the following format:
H W
H × W integers
H and W are integers that indicate the vertical and horizontal sizes of the chocolate bar, respectively. The H × W integers represent the block information, consisting of '0's that represent blocks without heart marks and '1's that represent blocks with heart marks.
The end of the input is indicated by a dataset where H = W = 0. Do not output anything for this case.
1 ≤ H, W ≤ 12, and it is assumed that the number of blocks with heart marks is six or less.
Output
For each dataset, output the maximum number of blocks that Taro can eat.
Sample Input
4 4
1 0 0 0
0 0 1 0
0 1 0 0
1 0 0 1
1 1
1
2 3
1 0 0
0 0 1
0 0	 
Output for the Sample Input
7
0
2

Extracted Specification

An integer H (1 ≤ H ≤ 12), representing some quantity or size.
An integer W (1 ≤ W ≤ 12), representing some quantity or size.
A list of H lists, each containing W integers, where each integer is either 0 or 1.

### Example Input:

```
4 4
1 0 0 0
0 0 1 0
0 1 0 0
1 0 0 1
1 1
1
2 3
1 0 0
0 0 1
0 0
0 0
```

### Function Signature:
Write a function f(inputs) that takes in the input. 
def f(inputs: List[Tuple[int, int, List[List[int]]]]): 
    ''' 
    inputs: a list of tuples, where each tuple consists of:
        - an integer H
        - an integer W
        - a list of H lists, each containing W integers
    '''

Test Case Generator

Certainly! Let's create a test case generator for the problem "Chocolate with Heart Marks."

The generator will create a valid matrix (chocolate bar) of size \( H \times W \) with the given constraints. Since the number of blocks with heart marks is at most six, the generator will ensure that condition is met.

Here's the Python function to generate test cases:

```python
import random

def generate_matrix(H, W, max_hearts=6):
    # Initialize the matrix with zeros
    matrix = [[0 for _ in range(W)] for _ in range(H)]
    
    # Place the hearts (1s) randomly in the matrix
    hearts_count = random.randint(1, min(max_hearts, H * W))
    hearts_positions = random.sample(range(H * W), hearts_count)
    
    for pos in hearts_positions:
        row = pos // W
        col = pos % W
        matrix[row][col] = 1
    
    return matrix

def tcgen():
    test_cases = []
    
    while True:
        # Randomly decide if we should add another test case or stop
        if random.random() < 0.2:  # 20% chance to stop
            break
        
        # Generate random H and W within the required range
        H = random.randint(1, 12)
        W = random.randint(1, 12)
        
        # Generate the matrix
        matrix = generate_matrix(H, W)
        
        test_cases.append((H, W, matrix))
    
    # Add the terminating case
    test_cases.append((0, 0, []))
    
    return test_cases

# Example usage:
test_cases = tcgen()
for case in test_cases:
    H, W, matrix = case
    print(f"{H} {W}")
    for row in matrix:
        print(' '.join(map(str, row)))
```

### Explanation:

1. **generate_matrix(H, W, max_hearts=6):**
    - Initializes an \( H \times W \) matrix filled with zeros.
    - Randomly selects a number of hearts (1s) to place in the matrix, ensuring that the number is at most `max_hearts`.
    - Randomly distributes the hearts within the matrix.

2. **tcgen():**
    - Creates a list of test cases.
    - Uses a loop to generate multiple test cases with a random chance of stopping.
    - For each test case, it generates random values for \( H \) and \( W \) within the specified range.
    - Calls `generate_matrix` to create the matrix for each test case.
    - Ensures the addition of the terminating case `(0, 0, [])`.
  
3. **Example usage:**
    - This block demonstrates how to use the `tcgen` function to generate test cases and print them in the required format.

This generator will produce various test cases, including both small and large matrices, and ensure that there's a mix of cases with different numbers of heart-marked blocks.

Extract Arguments

def extract_arguments(fh):
    datasets = []
    while True:
        H, W = map(int, fh.readline().strip().split())
        if H == 0 and W == 0:
            break
        matrix = []
        for _ in range(H):
            row = list(map(int, fh.readline().strip().split()))
            matrix.append(row)
        datasets.append((H, W, matrix))
    return datasets