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 (1 ≤ H ≤ 100), representing some quantity.
An integer W (1 ≤ W ≤ 100), representing some quantity.
A list of H strings, each of length W, containing only the characters ' ', '@', '#', '*'.
### Example Input:
```
10 10
####
#####@@
@#
@@@@@#*#*
@##***@@@*
#****#*@**
##@*#@@*##
*@@@@*@@@#
***#@*@##*
*@@@*@@##@
*@*#*@##**
@****#@@#@
0 0
```
### Function Signature:
Write a function f(inputs) that takes in a list of tuples, where each tuple represents a dataset. Each tuple consists of an integer H, an integer W, and a list of H strings.
def f(inputs: List[Tuple[int, int, List[str]]]):
'''
inputs: a list of tuples, each containing an integer H, an integer W, and a list of H strings
'''
Test Case Generator
Certainly! Below is a Python function that generates test cases for the "Land Distribution" problem:
```python
import random
def generate_orchard(H, W):
chars = [' ', '@', '#', '*']
orchard = []
for _ in range(H):
row = ''.join(random.choice(chars) for _ in range(W))
orchard.append(row)
return orchard
def tcgen():
datasets = []
num_datasets = random.randint(1, 20)
for _ in range(num_datasets):
H = random.randint(1, 10)
W = random.randint(1, 10)
orchard = generate_orchard(H, W)
dataset = (H, W, orchard)
datasets.append(dataset)
# Adding the terminating dataset
datasets.append((0, 0, []))
return datasets
def format_dataset(dataset):
H, W, orchard = dataset
if H == 0 and W == 0:
return "0 0"
formatted_orchard = "\n".join(orchard)
return f"{H} {W}\n{formatted_orchard}"
# Example usage
if __name__ == "__main__":
test_cases = tcgen()
for dataset in test_cases:
print(format_dataset(dataset))
print()
```
Explanation:
1. **generate_orchard()**: Generates a random orchard map with given dimensions H and W, filled with characters representing apples ('@'), oysters ('#'), oranges ('*'), and whitespace (' ').
2. **tcgen()**: Generates multiple datasets, each containing the dimensions H and W, and the corresponding orchard map. The number of datasets is randomly chosen between 1 and 20. The end condition of '0 0' is appended to signify the end of input.
3. **format_dataset()**: This helper function formats a dataset into a string suitable for printing or further processing.
The **Example usage** section demonstrates how to generate and print the test cases. Each dataset is printed in the required format, followed by a blank line for readability.
Extract Arguments
def extract_arguments(fh):
datasets = []
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)]
datasets.append((H, W, orchard))
return datasets
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# inputs = extract_arguments(fh)
# for dataset in inputs:
# f(*dataset)