Problem solution · Python

ABC322 D — Polyomino

ABC322 D — Polyomino: a Python solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Depth-first search
Source
KATO-Hiro AtCoder Solutions
Length
93 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

Depth-first search

For ABC322 D — Polyomino, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 93 lines of Python from the credited upstream file abc322_d.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected, together with recursive traversal.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

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 codeABC322 D — Polyomino · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  from copy import deepcopyfrom typing import List  # See:# https://kazun-kyopro.hatenablog.com/entry/ABC/298/Bdef rotate_90_degrees_to_right(array: List[List]):    new_array = [list(ai)[::-1] for ai in zip(*array)]     return new_array  def main():    import sys     sys.setrecursionlimit(10**8)     input = sys.stdin.readline     n = 3    size = 4    polyominos = list()     for _ in range(n):        pi = [list(input().rstrip()) for _ in range(size)]        polyominos.append(pi)     rotate_count = 3     h, w = 4, 4     # ポリオミノをシフトさせて配置できるか?    def can_put_polyomino(grid, polyomino, dy, dx):        for y, pi in enumerate(polyomino):            for x, pij in enumerate(pi):                if pij != "#":                    continue                 ny, nx = y + dy, x + dx                 if not (0 <= nx < w):                    return False                if not (0 <= ny < h):                    return False                if grid[ny][nx]:                    return False                 grid[ny][nx] = True         return True     def dfs(i, grid, ps):        if i == rotate_count:            ok = True             for y in range(size):                for x in range(size):                    if not grid[y][x]:                        ok = False                        break             if ok:                print("Yes")                exit()             return         for dy in range(-size + 1, size):            for dx in range(-size + 1, size):                grid2 = deepcopy(grid)                flag = can_put_polyomino(grid2, ps[i], dy, dx)                 if flag:                    dfs(i + 1, grid2, ps)     # 1番目を固定して、2番目・3番目を回転させる    # 正方形なので、1番目は回転が不要    for second in range(rotate_count + 1):        for third in range(rotate_count + 1):            dfs(0, [[False for _ in range(size)] for _ in range(size)], polyominos)            polyominos[2] = rotate_90_degrees_to_right(deepcopy(polyominos[2]))         polyominos[1] = rotate_90_degrees_to_right(deepcopy(polyominos[1]))     print("No")  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 ↗