Problem p01881 - Generation 1

Orig Description

Help the Princess!
The people of a certain kingdom make a revolution against the bad government of the princess. The revolutionary army invaded the royal palace in which the princess lives. The soldiers of the army are exploring the palace to catch the princess. Your job is writing a program to decide that the princess can escape from the royal palace or not.
 For simplicity, the ground of the palace is a rectangle divided into a grid. There are two kinds of cells in the grid: one is a cell that soldiers and the princess can enter, the other is a cell that soldiers or the princess cannot enter. We call the former an empty cell, the latter a wall. The princess and soldiers are in different empty cells at the beginning. There is only one escape hatch in the grid. If the princess arrives the hatch, then the princess can escape from the palace. There are more than or equal to zero soldiers in the palace.
The princess and all soldiers take an action at the same time in each unit time. In other words, the princess and soldiers must decide their action without knowing a next action of the other people. In each unit time, the princess and soldiers can move to a horizontally or vertically adjacent cell, or stay at the current cell. Furthermore the princess and soldiers cannot move out of the ground of the palace. If the princess and one or more soldiers exist in the same cell after their move, then the princess will be caught. It is guaranteed that the princess can reach the escape hatch via only empty cells if all soldiers are removed from the palace.
If there is a route for the princess such that soldiers cannot catch the princess even if soldiers make any moves, then the princess can escape the soldiers. Note that if the princess and a soldier arrive the escape hatch at the same time, the princess will be caught. Can the princess escape from the palace?
Input
Each dataset is formatted as follows.
$H$ $W$
$map_1$
$map_2$
...
$map_H$
The first line of a dataset contains two positive integers $H$ and $W$ delimited by a space, where $H$ is the height of the grid and $W$ is the width of the grid ($2 \leq H, W \leq 200$).
The $i$-th line of the subsequent $H$ lines gives a string $map_i$, which represents situation in the ground of palace.
$map_i$ is a string of length $W$, and the $j$-th character of $map_i$ represents the state of the cell of the $i$-th row and the $j$-th column.
'@', '\$', '%', '.', and '#' represent the princess, a soldier, the escape hatch, an empty cell, and a wall, respectively. It is guaranteed that there exists only one '@', only one '%', and more than or equal to zero '\$' in the grid.
Output
Output a line containing a word "Yes", if the princess can escape from the palace. Otherwise, output "No".
Sample Input 1
2 4
%.@\$
..\$\$
Output for the Sample Input 1
Yes
Sample Input 2
3 4
.%..
.##.
.@\$.
Output for the Sample Input 2
Yes
Sample Input 3
2 3
%\$@
###
Output for the Sample Input 3
No
Sample Input 4
2 3
@#\$
.%.
Output for the Sample Input 4
No
Sample Input 5
2 2
@%
..
Output for the Sample Input 5
Yes

Extracted Specification

### Canonicalized Input Description:

An integer H (2 ≤ H ≤ 200), representing some quantity or size.
An integer W (2 ≤ W ≤ 200), representing some quantity or size.
A list of H strings of length W, containing characters from the set {'@', '$', '%', '.', '#'}. 

### Example Input:

```
2 4
%.@$
..$$
```

### Function Signature:

Write a function f(H, W, grid) that takes in the input.

```python
def f(H: int, W: int, grid: List[str]):
    '''
    H: an integer
    W: an integer
    grid: a list of strings
    '''
```

Test Case Generator

Sure! Here is a test case generator for the "Help the Princess!" problem:

```python
import random

# Constants for grid characters
PRINCESS = '@'
SOLDIER = '$'
HATCH = '%'
EMPTY = '.'
WALL = '#'

def generate_grid(H, W):
    """
    Generates a grid with H rows and W columns.
    Ensures there is exactly one princess, one escape hatch, and one or more soldiers.
    """
    grid = [[EMPTY for _ in range(W)] for _ in range(H)]

    # Random positions for princess, hatch, and soldiers
    princess_pos = (random.randint(0, H-1), random.randint(0, W-1))
    hatch_pos = (random.randint(0, H-1), random.randint(0, W-1))

    # Ensure princess and hatch are in different positions
    while hatch_pos == princess_pos:
        hatch_pos = (random.randint(0, H-1), random.randint(0, W-1))
    
    # Place princess and hatch in the grid
    grid[princess_pos[0]][princess_pos[1]] = PRINCESS
    grid[hatch_pos[0]][hatch_pos[1]] = HATCH

    # Random number of soldiers
    num_soldiers = random.randint(0, H*W - 2)  # Remaining cells for soldiers

    # Place soldiers in the grid
    for _ in range(num_soldiers):
        soldier_pos = (random.randint(0, H-1), random.randint(0, W-1))
        while grid[soldier_pos[0]][soldier_pos[1]] != EMPTY:
            soldier_pos = (random.randint(0, H-1), random.randint(0, W-1))
        grid[soldier_pos[0]][soldier_pos[1]] = SOLDIER

    # Randomly place some walls
    for _ in range(random.randint(0, H*W // 4)):
        wall_pos = (random.randint(0, H-1), random.randint(0, W-1))
        while grid[wall_pos[0]][wall_pos[1]] != EMPTY:
            wall_pos = (random.randint(0, H-1), random.randint(0, W-1))
        grid[wall_pos[0]][wall_pos[1]] = WALL

    return grid

def tcgen():
    H = random.randint(2, 200)
    W = random.randint(2, 200)
    grid = generate_grid(H, W)
    
    # Convert grid to list of strings
    grid_str = [''.join(row) for row in grid]
    
    return H, W, grid_str

# Example of generating a test case
H, W, grid_str = tcgen()
print(H, W)
for row in grid_str:
    print(row)
```

### Explanation:

1. **Constants for Grid Characters:**
   - `PRINCESS`, `SOLDIER`, `HATCH`, `EMPTY`, and `WALL` are used to represent the different characters in the grid.

2. **generate_grid Function:**
   - This function generates a grid of size `H` x `W`.
   - It ensures that there is exactly one princess (`'@'`), one escape hatch (`'%'`), and one or more soldiers (`'$'`).
   - It places the princess and the hatch in different positions.
   - Then, it places the soldiers in random empty cells.
   - Random walls are also placed in the grid.

3. **tcgen Function:**
   - This function generates random values for `H` and `W` within the specified range (2 to 200).
   - It then calls `generate_grid` to create the grid.
   - The grid is then converted from a list of lists to a list of strings, as required by the problem statement.

4. **Example Usage:**
   - The example at the end demonstrates how to generate and print a test case using the `tcgen` function.

This will generate random valid test cases for the "Help the Princess!" problem.

Extract Arguments

def extract_arguments(fh):
    H, W = map(int, fh.readline().strip().split())
    palace_map = [fh.readline().strip() for _ in range(H)]
    return H, W, palace_map

# if __name__ == "__main__":
#     input_path = sys.argv[1]
#     with open(input_path, 'r') as fh: 
#         H, W, palace_map = extract_arguments(fh)
#         f(H, W, palace_map)