Orig Description
Land Distribution
Mr. Tanaka has passed away and left a field of HW acres that is divided into HxW squares in the orchard. The orchard has three types of plants: apples, oysters, and oranges, and each square contains one of them. Mr. Tanaka left the inheritance instruction as follows:
Distribute the land as much as possible to the relatives, but if two adjacent trees are the same, the border of the land is not determined, and they are treated as one plot of land.
For example, a 3x10 orchard ('リ' represents apples, 'カ' represents oysters, and 'ミ' represents oranges) is shown in the following figure.
Adjust the boundary by removing the same species of planted trees, resulting in the following figure.
Therefore, it can be divided into 10 plots, corresponding to at most 10 people.
The distribution must be completed before the border between each plot is no longer visible due to snow (i.e., before winter). The objective of completing the distribution task is to determine how many relatives can be assigned land based on the orchard map.
Write a program that reads the orchard map and outputs the number of relatives who will receive land.
Input
Multiple data sets will be provided; the input ends with 0 0. Each data set starts with a line containing H and W. Separate them by a single space. The next H lines contain H × W characters. The string shall only contain the following characters: whitespace, "@" representing an apple, "#" representing an oyster, "*" representing an orange.
No more than 20 datasets will be provided.
Output
For each test dataset, output the number of relatives who will receive land in one line.
Sample Input
10 10
####
#####@@
@#
@@@@@#*#*
@##***@@@*
#****#*@**
##@*#@@*##
*@@@@*@@@#
***#@*@##*
*@@@*@@##@
*@*#*@##**
@****#@@#@
0 0
"""
Sample Output
33
Extracted Specification
An integer H (0 ≤ H ≤ 100), representing some quantity.
An integer W (0 ≤ W ≤ 100), representing some quantity.
A list of H strings, each of length W, containing only the characters '@', '#', '*', or ' ' (whitespace).
### Example Input:
```
10 10
####
#####@@
@#
@@@@@#*#*
@##***@@@*
#****#*@**
##@*#@@*##
*@@@@*@@@#
***#@*@##*
*@@@*@@##@
*@*#*@##**
@****#@@#@
0 0
```
### Function Signature:
Write a function f(inputs) that takes in the input.
```python
def f(inputs: List[Tuple[int, int, List[str]]]):
'''
inputs: a list of tuples, where each tuple contains:
- an integer H
- an integer W
- a list of H strings
'''
```
Test Case Generator
Sure, let's create a test case generator for the "Land Distribution" problem.
To generate the test cases, we need to:
1. Randomly choose values for \(H\) (height) and \(W\) (width) within the appropriate range.
2. Generate an orchard map with random characters ('@', '#', '*') and whitespace.
3. Ensure that the input ends with '0 0'.
Here is a Python function to generate such test cases:
```python
import random
def generate_orchard_map(H, W):
plants = ['@', '#', '*', ' ']
orchard = []
for _ in range(H):
row = ''.join(random.choice(plants) for _ in range(W))
orchard.append(row)
return orchard
def tcgen():
datasets = []
max_datasets = 20
for _ in range(random.randint(1, max_datasets)):
H = random.randint(1, 20)
W = random.randint(1, 20)
orchard_map = generate_orchard_map(H, W)
datasets.append((H, W, orchard_map))
# Ensure we end with the '0 0' line
datasets.append((0, 0, []))
return datasets
# Format the output as required by the problem statement
def format_test_case(datasets):
result = []
for H, W, orchard_map in datasets:
if H == 0 and W == 0:
result.append('0 0')
else:
result.append(f"{H} {W}")
result.extend(orchard_map)
return '\n'.join(result)
# Example usage
datasets = tcgen()
formatted_test_case = format_test_case(datasets)
print(formatted_test_case)
```
### Explanation:
1. **generate_orchard_map(H, W)**:
- This function generates an orchard map of size \( H \times W \).
- Each cell in the map is randomly chosen from the set of valid characters: '@', '#', '*', ' '.
2. **tcgen()**:
- This function generates multiple datasets (up to 20) with random values for \( H \) and \( W \).
- For each dataset, it creates an orchard map using the `generate_orchard_map` function.
- It ensures that the input ends with the line '0 0' to indicate the termination of input.
3. **format_test_case(datasets)**:
- This function formats the generated datasets into a string that matches the expected input format for the problem.
- Each dataset is represented by its height and width on a line, followed by the orchard map rows.
- The output is joined into a single string with newline characters.
You can run the `tcgen()` function to generate random test cases and then format them using `format_test_case(datasets)` to get the desired input format for testing.
Extract Arguments
def extract_arguments(fh):
data_sets = []
while True:
H, W = map(int, fh.readline().strip().split())
if H == 0 and W == 0:
break
orchard = [fh.readline().strip() for _ in range(H)]
data_sets.append((H, W, orchard))
return data_sets
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# data_sets = extract_arguments(fh)
# for H, W, orchard in data_sets:
# f(H, W, orchard)