Problem solution · Python

ABC339 D — Synchronized Players

ABC339 D — Synchronized Players : 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
104 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 ABC339 D — Synchronized Players , 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

  • 104 lines of Python from the credited upstream file abc339_d.py.
  • The implementation visibly relies on sequence storage, 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 codeABC339 D — Synchronized Players · 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     n = int(input())    s = [list(input().rstrip()) for _ in range(n)]    pos = []     for i in range(n):        for j in range(n):            if s[i][j] == "P":                pos.append((i, j))     # グリッドの状態を頂点とするグラフの最短経路問題に帰着 = BFS     # 定数倍が重いので、座標のハッシュ値をキューに入れる    # n進数に変換    def to_hash(pos):        p00, p01 = pos[0]        p10, p11 = pos[1]         value = p00        value = value * n + p01        value = value * n + p10        value = value * n + p11         return value     def to_pos(hash):        # to_hashとは逆順に        p11 = hash % n        hash //= n        p10 = hash % n        hash //= n         p01 = hash % n        hash //= n        p00 = hash % n        hash //= n         pos = [(p00, p01), (p10, p11)]         return pos     n4 = n**4    inf = 10**9     dist = [inf] * n4    id = to_hash(pos)    dist[id] = 0     q = deque([id])    dxy = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (1, -1), (-1, 1), (1, 1)]    dxy = dxy[:4]     while q:        cur_id = q.popleft()        pos = to_pos(cur_id)        d = dist[cur_id]         # グラフを陽に持たず、1手先の状態を用意        for dx, dy in dxy:            npos = []             for y, x in pos:                nx = x + dx                ny = y + dy                 # 条件を満たしていなければ元に戻す                if nx < 0 or nx >= n or ny < 0 or ny >= n or s[ny][nx] == "#":                    nx, ny = x, y                 npos.append((ny, nx))             nid = to_hash(npos)             if dist[nid] != inf:                continue             dist[nid] = d + 1            q.append(nid)     ans = inf     for id, di in enumerate(dist):        pos = to_pos(id)         if pos[0] == pos[1]:            ans = min(ans, di)     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 ↗