Problem solution · Python

ABC348 D — Medicines on Grid

ABC348 D — Medicines on Grid: a Python solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Breadth-first search
Source
KATO-Hiro AtCoder Solutions
Length
119 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For ABC348 D — Medicines on Grid, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 119 lines of Python from the credited upstream file abc348_d.py.
  • The implementation visibly relies on sequence storage, ordered lookup, work queue.
  • No explicit loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC348 D — Medicines on Grid · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def main():    import sys    from collections import deque    from typing import Any, List, Tuple     input = sys.stdin.readline     h, w = map(int, input().split())    grid = [list(input().rstrip()) for _ in range(h)]    n = int(input())    medicines = list()     sy, sx, ty, tx = 0, 0, 0, 0     for i in range(h):        for j in range(w):            if grid[i][j] == "S":                sy, sx = i, j            elif grid[i][j] == "T":                ty, tx = i, j     # スタートとゴールにもエネルギーが0の薬があるとみなす    for _ in range(n):        ri, ci, ei = list(map(int, input().split()))        ri -= 1        ci -= 1        medicines.append((ri, ci, ei))     medicines.append((sy, sx, 0))    medicines.append((ty, tx, 0))    inf = 10**12     # 各薬を頂点とするグラフをBFSで求める    def bfs_for_grid(        grid: List[List[Any]], h: int, w: int, sy: int = 0, sx: int = 0    ) -> List[List[int]]:        d = deque()        d.append((sy, sx))        pending = inf        dist = [[pending] * w for _ in range(h)]        dist[sy][sx] = 0  # Initialize        dxy = [(-1, 0), (1, 0), (0, -1), (0, 1)]         while d:            y, x = d.popleft()             if dist[y][x] == pending:                continue             for dx, dy in dxy:                nx = x + dx                ny = y + dy                 if nx < 0 or nx >= w or ny < 0 or ny >= h:                    continue                if grid[ny][nx] == "#":                    continue                if dist[ny][nx] != pending:                    continue                 dist[ny][nx] = dist[y][x] + 1  # Update ans                d.append((ny, nx))         return dist     to = [[] for _ in range(n + 2)]     for i, (ri, ci, ei) in enumerate(medicines):        dist = bfs_for_grid(grid=grid, h=h, w=w, sy=ri, sx=ci)         # 薬jまで到達できるか判定        for j, (rj, cj, _) in enumerate(medicines):            if i == j:                continue             if dist[rj][cj] <= ei:                to[i].append(j)     # 到達判定    def bfs_for_graph(        vertex_count: int, graph: List[List[int]], start_id: int    ) -> Tuple[List[bool], List[int]]:        d = deque([start_id])        visited = [False] * vertex_count        dist = [inf] * vertex_count        dist[start_id] = 0         while d:            cur = d.pop()             if visited[cur]:                continue             visited[cur] = True             for to in graph[cur]:                if visited[to]:                    continue                 d.append(to)                dist[to] = min(dist[to], dist[cur] + 1)         return visited, dist     start_id = -2    _, reachables = bfs_for_graph(vertex_count=n + 2, graph=to, start_id=start_id)     if reachables[-1] == inf:        print("No")    else:        print("Yes")  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗