#K56572. Taco Treasure Hunt

    ID: 30228 Type: Default 1000ms 256MiB

Taco Treasure Hunt

Taco Treasure Hunt

You are given an N x N grid where each cell is represented by one of the following characters:

  • . for an empty cell,
  • # for an obstacle, and
  • * for a treasure.

You start from a specified cell (using 1-indexed coordinates) and can move in the four cardinal directions (up, down, left, right). You cannot move through obstacles. When you enter a cell containing a treasure, you collect it. Your task is to determine the maximum number of treasures you can collect by moving through all accessible cells.

The movements are governed by the following rule: once a cell is visited, it cannot be visited again. If the starting cell contains a treasure, it should be counted as collected.

The problem can be formalized as follows:

Find the maximum number of treasures you can collect, starting at cell (x, y) and exploring the grid using valid moves. Use a Breadth-First Search (BFS) algorithm to traverse the reachable area of the grid.

Specifically, using LaTeX notation, if we denote the grid as \(G\) with size \(N \times N\), and the starting position (after converting from 1-indexed to 0-indexed) as \((x_0, y_0)\), then your goal is to compute:

\[ T = \sum_{(i,j) \in \text{Reachable}(x_0,y_0)} \mathbf{1}_{\{G_{i,j}=*\}}, \]

where \(\text{Reachable}(x_0,y_0)\) is the set of cells reachable from \((x_0, y_0)\) without passing through obstacles, and \(\mathbf{1}_{\{\cdot\}}\) is the indicator function.

inputFormat

The input is read from stdin and is structured as follows:

  1. The first line contains an integer T representing the number of test cases.
  2. For each test case:
    1. The first line contains an integer N denoting the size of the grid.
    2. The next N lines each contain a string of length N representing a row of the grid. The characters can be ., #, or *.
    3. The following line contains two space-separated integers x and y indicating the starting position (1-indexed).

outputFormat

For each test case, output a single line to stdout that contains an integer representing the maximum number of treasures that can be collected.

## sample
3
5
.....
.#*#.
..*..
#..*#
.....
1 1
5
.....
.#.#.
.....
#...#
.....
1 1
5
.*.#.
.#*#.
..*..
#..*#
.....
5 5
3

0 4

</p>