公式

B - 倉庫の点検ロボット / Warehouse Inspection Robot 解説 by admin

Qwen3-Coder-480B

Overview

A robot moves on a grid according to given instructions, collecting items from visited cells. We simulate this process and find the number of items remaining at the end.

Analysis

This problem requires simulating the robot’s movement while efficiently managing the items on visited cells.

Key Observations

  • The robot moves sequentially according to instructions and collects items from each cell it visits on the spot.
  • Since items on each cell do not reappear once collected, we need to track whether each cell’s item has already been collected.
  • If a move would take the robot outside the grid, that instruction is ignored.

Issues with the Naive Approach

  • Updating the current position for each instruction and directly checking whether the cell is # works in principle, but if we check the cell state using string operations every time, the string manipulation becomes expensive and can cause TLE.
  • Additionally, reading item existence from strings each time is inefficient.

Improvements

  • By preprocessing the entire grid state (where items are located) into a two-dimensional array, we can access it efficiently.
  • We manage whether an item has been collected using flags, updating the flag when the robot visits that cell.
  • By counting the total number of items in advance, we can simply subtract each time an item is collected to easily determine the final remaining count.

Algorithm

  1. Read the warehouse state from input, count the number of # cells, and record them in a two-dimensional array warehouse (1: item present, 0: no item).
  2. Place the robot at the initial position \((0, 0)\), and if there is an item at that cell, collect it.
  3. Process the instruction string \(T\) from the beginning in order:
    • Calculate the next coordinates according to each instruction.
    • If the next coordinates are within the grid, move there, and if there is an item at that cell, collect it (update warehouse and decrement the count).
  4. Output the number of remaining items after all instructions have been processed.

Complexity

  • Time complexity: \(O(H \cdot W + N)\) (\(O(H \cdot W)\) for the initial item count, \(O(N)\) for processing instructions)
  • Space complexity: \(O(H \cdot W)\) (two-dimensional array to store the warehouse state)

Implementation Notes

  • Coordinates are given in 1-indexed format, but it is common to handle them as 0-indexed internally in the program.

  • Always check whether the destination is out of bounds, and do not move if it is.

  • Manage item presence using a two-dimensional list, and be careful not to collect from an already-collected cell again.

    Source Code

H, W, N = map(int, input().split())
S = [input() for _ in range(H)]
T = input()

# 荷物の初期位置を記録
warehouse = [[0] * W for _ in range(H)]
initial_count = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            warehouse[i][j] = 1
            initial_count += 1

# ロボットの初期位置 (0-indexed)
x, y = 0, 0

# 最初のマスの荷物を回収
if warehouse[x][y] == 1:
    warehouse[x][y] = 0
    initial_count -= 1

# 命令を順に処理
for move in T:
    nx, ny = x, y
    if move == 'U':
        nx -= 1
    elif move == 'D':
        nx += 1
    elif move == 'L':
        ny -= 1
    elif move == 'R':
        ny += 1
    
    # グリッド内かどうかチェック
    if 0 <= nx < H and 0 <= ny < W:
        x, y = nx, ny
        # 荷物があれば回収
        if warehouse[x][y] == 1:
            warehouse[x][y] = 0
            initial_count -= 1

print(initial_count)

This editorial was generated by qwen3-coder-480b.