B - 倉庫の点検ロボット / Warehouse Inspection Robot Editorial by admin Editorial for printing
Gemini 3.0 FlashOverview
This is a problem where you simulate the movement of a robot on an \(H \times W\) grid following \(N\) given instructions, and determine the number of items that remain uncollected at the end.
Analysis
This problem can be solved by simulation, executing instructions one by one while tracking the robot’s current position.
The key points are as follows:
- Processing the initial position: Before executing any instructions, you first need to collect the item at the starting square \((1, 1)\).
- Movement validation: If an instruction would move the robot outside the grid (row less than \(1\) or greater than \(H\), column less than \(1\) or greater than \(W\)), that instruction is ignored and the robot does not move.
- Collecting items: When the robot visits a square containing an item, that square’s state is changed to “no item.” This prevents double-counting when the same square is visited again.
- Final counting: After all instructions have been executed, scan the entire grid and count how many # remain.
Checking the constraints, we have \(H, W \leq 500\) and \(N \leq 2 \times 10^5\). The instruction simulation takes \(O(N)\) and the final counting takes \(O(HW)\), both of which comfortably fit within the time limit.
Algorithm
- Read the warehouse state as a 2D array (e.g., a list of lists).
- Set the robot’s current position to \((r, c) = (0, 0)\) (managed with 0-indexed coordinates).
- If square \((r, c)\) contains an item (
#), replace it with empty (.). - For each character (instruction) in the string \(T\), repeat the following process:
- Depending on whether the instruction is
U,D,L, orR, compute the candidate destination \((nr, nc)\). - If \((nr, nc)\) is within the grid bounds (\(0 \leq nr < H\) and \(0 \leq nc < W\)), update the current position to \((nr, nc)\).
- If the updated square \((r, c)\) contains an item (
#), replace it with empty (.).
- Depending on whether the instruction is
- After all instructions have been processed, sum up the number of
#in the 2D array and output it.
Complexity
- Time complexity: \(O(N + HW)\)
- The instruction simulation takes \(O(N)\).
- Checking all squares in the grid to count the remaining items takes \(O(HW)\).
- Space complexity: \(O(HW + N)\)
- \(O(HW)\) of memory is used to store the grid state, and \(O(N)\) to store the instruction string.
Implementation Notes
Conversion to 0-indexed: The problem statement uses squares starting from \((1, 1)\), but handling them as starting from \((0, 0)\) in the program (for array indices, etc.) makes the processing smoother.
Updating items: Since strings in Python are immutable, to write something like
grid[r][c] = '.', you need to convert each row into a list of characters beforehand.Boundary checking: At each move, you must correctly verify that the destination coordinates do not become less than
0or equal to or greater thanHorW.Source Code
import sys
def solve():
# 全ての入力を読み込み、空白文字で分割します。
# これにより、H, W, N、グリッドの各行、および命令文字列 T を取得します。
data = sys.stdin.read().split()
if not data:
return
# グリッドの高さ H、幅 W、命令の数 N を取得
H = int(data[0])
W = int(data[1])
N = int(data[2])
# グリッドの状態を 2次元リストとして読み込みます。
# 荷物の回収('#' から '.' への変更)を行うため、リストのリストにします。
# data[3] から data[H+2] までが各行 S_i に対応します。
grid = [list(data[i]) for i in range(3, 3 + H)]
# 命令文字列 T を取得します。
# 万が一命令が複数行に分かれていた場合を考慮して join します。
T = "".join(data[3 + H:])
# ロボットの初期位置 (0-indexed)
# 問題文の (1, 1) は 0-indexed では (0, 0) になります。
r, c = 0, 0
# 最初に配置されたマスの荷物を回収します。
if grid[r][c] == '#':
grid[r][c] = '.'
# 各命令を順番に実行します。
for cmd in T:
nr, nc = r, c
if cmd == 'U':
nr -= 1
elif cmd == 'D':
nr += 1
elif cmd == 'L':
nc -= 1
elif cmd == 'R':
nc += 1
# 移動後のマスがグリッドの範囲内にあるか判定します。
if 0 <= nr < H and 0 <= nc < W:
# 範囲内であれば移動し、そのマスの荷物を回収します。
r, c = nr, nc
if grid[r][c] == '#':
grid[r][c] = '.'
# 範囲外に出る命令の場合は無視され、ロボットはその場に留まります。
# すべての命令終了後、残っている荷物 ('#') の個数を数えます。
remaining_cargo = 0
for row in grid:
remaining_cargo += row.count('#')
# 結果を出力します。
print(remaining_cargo)
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3-flash-preview.
posted:
last update: