Problem solution · Python

ABC460 D — Repeatedly Repainting

ABC460 D — Repeatedly Repainting : 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
86 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 ABC460 D — Repeatedly Repainting , 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

  • 86 lines of Python from the credited upstream file abc460_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 codeABC460 D — Repeatedly Repainting · 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)]    dxy = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (1, -1), (-1, 1), (1, 1)]     for _ in range(2):        t = [["." for _ in range(w)] for _ in range(h)]         for i in range(h):            for j in range(w):                if s[i][j] != "#":                    continue                 for dx, dy in dxy:                    ny, nx = i + dy, j + dx                     if not (0 <= nx < w):                        continue                    if not (0 <= ny < h):                        continue                    if s[ny][nx] == "#":                        continue                     t[ny][nx] = "#"         s = t[:]     q = deque()    inf = 10**18 + 1    dist = [[inf for _ in range(w)] for _ in range(h)]     for i in range(h):        for j in range(w):            if s[i][j] == ".":                continue             q.append((i, j))            dist[i][j] = 0     visited = [[False for _ in range(w)] for _ in range(h)]     while q:        y, x = q.popleft()         if visited[y][x]:            continue         visited[y][x] = True         for dx, dy in dxy:            ny, nx = y + dy, x + dx             if not (0 <= nx < w):                continue            if not (0 <= ny < h):                continue            if dist[ny][nx] != inf:                continue             dist[ny][nx] = dist[y][x] + 1            q.append((ny, nx))     ans = [["." for _ in range(w)] for _ in range(h)]     for i in range(h):        for j in range(w):            if dist[i][j] % 2 == 1:                continue             ans[i][j] = "#"     for ans_i in ans:        print("".join(ans_i))  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 ↗