Problem solution · Python

ABC400 D — Takahashi the Wall Breaker

ABC400 D — Takahashi the Wall Breaker: 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
61 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 ABC400 D — Takahashi the Wall Breaker, 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

  • 61 lines of Python from the credited upstream file abc400_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 codeABC400 D — Takahashi the Wall Breaker · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def main():    import sys    from collections import deque     input = sys.stdin.readline     h, w = map(int, input().split())    s = [list(input().rstrip()) for _ in range(h)]    sy, sx, ty, tx = map(lambda x: int(x) - 1, input().split())    q = deque()    inf = 10**18    dist = [[inf for __ in range(w)] for _ in range(h)]    visited = [[False for __ in range(w)] for _ in range(h)]    dxy = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (1, -1), (-1, 1), (1, 1)]    dxy = dxy[:4]     # 01-BFSの問題と言い換え    def push(y, x, d, cost):        if not (0 <= y < h and 0 <= x < w):            return        if dist[y][x] <= d:            return         dist[y][x] = d         if cost == 0:            q.appendleft((y, x))        else:            q.append((y, x))     push(sy, sx, 0, 0)     while q:        cur_y, cur_x = q.popleft()         if visited[cur_y][cur_x]:            continue         visited[cur_y][cur_x] = True         for dx, dy in dxy:            ny, nx = cur_y + dy, cur_x + dx             # 道 -> 道: コスト0            if (0 <= ny < h and 0 <= nx < w) and s[ny][nx] == ".":                push(ny, nx, dist[cur_y][cur_x], 0)             # 道 -> 壁: コスト1            push(cur_y + dy, cur_x + dx, dist[cur_y][cur_x] + 1, 1)            push(cur_y + 2 * dy, cur_x + 2 * dx, dist[cur_y][cur_x] + 1, 1)     ans = dist[ty][tx]    print(ans)  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 ↗