Official

B - 宝物の地図 / Treasure Map Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

For each row, check whether the string contains T. If it does, represent it as o; if it does not, represent it as x. Concatenate these results from the top row to the bottom row and print them.

Analysis

Since cells with buried treasure are represented by T, for each row, it is sufficient to check whether it contains at least one T. There is no need to count the treasures or determine their specific positions.

For example, if the rows are given as follows, the results will be:

  • ..T. → contains treasure, so o
  • .... → does not contain treasure, so x
  • TT.. → contains treasure, so o

If they appear in this order, the output is oxo.

Even a naive approach of checking every cell one by one easily runs well within the time limit, since there are at most \(H \times W = 100\) cells. No optimization is necessary.

However, note that if you append o every time you encounter a T, you will get a wrong answer when a row contains multiple treasures. You must output exactly one character per row.

Algorithm

  1. Read \(H, W\).
  2. From the top row to the bottom, repeat the following process \(H\) times:
    • Read the string representing the row.
    • If it contains T, append o to the result; otherwise, append x.
  3. Concatenate the resulting \(H\) characters and output them on a single line.

In Python, you can check whether a string s contains T using 'T' in s.

Complexity

  • Time Complexity: \(O(HW)\)
    • Inspecting at most \(W\) characters per row is performed for all \(H\) rows.
  • Space Complexity: \(O(H + W)\)
    • We handle the output string of length \(H\) and the row string of length \(W\) read from the input. There is no need to store the entire grid.

Implementation Details

In the solution code, the following expression reads one row of input and performs the check:

'o' if 'T' in input() else 'x'

We repeat this \(H\) times using for _ in range(h). The variable name _ is commonly used when the loop counter is not needed inside the loop body.

Finally, concatenate the results without any separator using "".join(...) and print it. Make sure not to insert spaces or newlines between o and x.

Note that although w is read, it is not directly used in the check because each input string is already guaranteed to have length \(W\).

Source Code

# 問題文から独立に実装
h, w = map(int, input().split())
print("".join('o' if 'T' in input() else 'x' for _ in range(h)))

This editorial was generated by gpt-6-astra-medium.

posted:
last update: