Problem solution · Python

ABC176 D — Wizard in Maze

ABC176 D — Wizard in Maze: 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
78 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 ABC176 D — Wizard in Maze, 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

  • 78 lines of Python from the credited upstream file abc176_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 codeABC176 D — Wizard in Maze · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def main():    from collections import deque     h, w = map(int, input().split())    sy, sx = map(int, input().split())    gy, gx = map(int, input().split())    sy -= 1    sx -= 1    gy -= 1    gx -= 1    s = [list(input()) for _ in range(h)]     dxy = [(-1, 0), (1, 0), (0, -1), (0, 1)]     INF = 10 ** 9    dist = [[INF for _ in range(w)] for __ in range(h)]    visited = [[False for _ in range(w)] for __ in range(h)]     d = deque()    d.append((sx, sy))    dist[sy][sx] = 0    visited[sy][sx] = True     while d:        xi, yi = d.popleft()         for dx, dy in dxy:            nx = xi + dx            ny = yi + dy             if nx < 0 or nx >= w:                continue            if ny < 0 or ny >= h:                continue            if s[ny][nx] == "#":                continue            if dist[ny][nx] <= dist[yi][xi]:                continue             visited[ny][nx] = True            dist[ny][nx] = dist[yi][xi]            d.appendleft((nx, ny))         for i in range(-2, 2 + 1):            for j in range(-2, 2 + 1):                nx = xi + i                ny = yi + j                 if nx < 0 or nx >= w:                    continue                if ny < 0 or ny >= h:                    continue                if s[ny][nx] == "#":                    continue                if dist[ny][nx] <= dist[yi][xi] + 1:                    continue                if visited[ny][nx]:                    continue                 visited[ny][nx] = True                dist[ny][nx] = dist[yi][xi] + 1                d.append((nx, ny))     ans = dist[gy][gx]     if ans == INF:        ans = -1     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 ↗