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.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- 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.
Use this to learn the idea, then write your own version.
12 3 4from copy import deepcopy5from typing import List6 7 8910def rotate_90_degrees_to_right(array: List[List]):11 new_array = [list(ai)[::-1] for ai in zip(*array)]12 13 return new_array14 15 16def main():17 import sys18 19 sys.setrecursionlimit(10**8)20 21 input = sys.stdin.readline22 23 n = 324 size = 425 polyominos = list()26 27 for _ in range(n):28 pi = [list(input().rstrip()) for _ in range(size)]29 polyominos.append(pi)30 31 rotate_count = 332 33 h, w = 4, 434 35 36 def can_put_polyomino(grid, polyomino, dy, dx):37 for y, pi in enumerate(polyomino):38 for x, pij in enumerate(pi):39 if pij != "#":40 continue41 42 ny, nx = y + dy, x + dx43 44 if not (0 <= nx < w):45 return False46 if not (0 <= ny < h):47 return False48 if grid[ny][nx]:49 return False50 51 grid[ny][nx] = True52 53 return True54 55 def dfs(i, grid, ps):56 if i == rotate_count:57 ok = True58 59 for y in range(size):60 for x in range(size):61 if not grid[y][x]:62 ok = False63 break64 65 if ok:66 print("Yes")67 exit()68 69 return70 71 for dy in range(-size + 1, size):72 for dx in range(-size + 1, size):73 grid2 = deepcopy(grid)74 flag = can_put_polyomino(grid2, ps[i], dy, dx)75 76 if flag:77 dfs(i + 1, grid2, ps)78 79 80 81 for second in range(rotate_count + 1):82 for third in range(rotate_count + 1):83 dfs(0, [[False for _ in range(size)] for _ in range(size)], polyominos)84 polyominos[2] = rotate_90_degrees_to_right(deepcopy(polyominos[2]))85 86 polyominos[1] = rotate_90_degrees_to_right(deepcopy(polyominos[1]))87 88 print("No")89 90 91if __name__ == "__main__":92 main()93