A - 星空の観測記録 / Starry Sky Observation Log Editorial by admin
Qwen3-Coder-480BOverview
Given grid-shaped observation data, find all positions of stars (T) and output them in the specified order.
Analysis
In this problem, observation data is given as an \(H \times W\) two-dimensional grid, where each cell is either . (nothing) or T (star). The goal is to enumerate all positions of T in order of “ascending row number, and ascending column number for ties.”
A straightforward approach is to scan the entire grid and find T positions one by one. This is efficient, and since a full search is feasible even with the constraint \(H, W \leq 1000\), there is no concern about TLE (Time Limit Exceeded).
What’s important is the coordinate system indexing. Since the problem defines the top-left as \((1, 1)\), if you use 0-indexed coordinates internally in your program, you need to add 1 to each when outputting.
Algorithm
- Read \(H\) and \(W\) from input, and store the following \(H\) lines of strings as a list.
- Scan the entire grid with a nested loop, and whenever
Tis found, convert its position to 1-indexed and save it to a list. - Output the number of stars, then output each star’s position in order.
Since the scanning order is “row-major, processing each row from left to right,” it naturally matches the order required by the problem (ascending by row number, then by column number). Therefore, there is no need to sort the results.
Complexity
- Time complexity: \(O(H \times W)\)
→ Because each cell of the grid is checked exactly once. - Space complexity: \(O(H \times W)\)
→ Memory usage is proportional to the size of the input grid.
Implementation Notes
- Coordinate conversion: Since Python internally uses 0-indexed, use
i+1,j+1when outputting. - Pay attention to the output format: first output the number of stars \(K\), then output one star’s position per line, separated by spaces.
- Since each row of the grid is read as a string, it can be accessed as
grid[i][j].
## Source Code
```python
H, W = map(int, input().split())
grid = [input() for _ in range(H)]
stars = []
for i in range(H):
for j in range(W):
if grid[i][j] == 'T':
stars.append((i + 1, j + 1))
print(len(stars))
for r, c in stars:
print(r, c)
This editorial was generated by qwen3-coder-480b.
posted:
last update: