Problem solution · Python

ABC407 D — Domino Covering XOR

ABC407 D — Domino Covering XOR: 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
71 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 ABC407 D — Domino Covering XOR, 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

  • 71 lines of Python from the credited upstream file abc407_d.py.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • 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 codeABC407 D — Domino Covering XOR · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def main():    import sys     sys.setrecursionlimit(10**8)     input = sys.stdin.readline     h, w = map(int, input().split())    a = [list(map(int, input().split())) for _ in range(h)]    xor_all = 0    used = [[False] * w for _ in range(h)]    dominos = list()    ans = 0     for i in range(h):        for j in range(w):            xor_all ^= a[i][j]     def dfs(row, col):        if row >= h:            candidate = xor_all             for r1, c1, r2, c2 in dominos:                candidate ^= a[r1][c1]                candidate ^= a[r2][c2]             nonlocal ans            ans = max(ans, candidate)             return         # 次のマスに進む        nr, nc = row, col + 1         if nc >= w:            nr, nc = nr + 1, 0         # 既に訪問済みなら次のマスに進む        if used[row][col]:            dfs(nr, nc)            return         # 現在のマスを選択しない        dfs(nr, nc)         # 1 * 2 のマスを置く        if (col + 1 < w) and not used[row][col + 1]:            used[row][col] = used[row][col + 1] = True            dominos.append((row, col, row, col + 1))            dfs(nr, nc)            dominos.pop()            used[row][col] = used[row][col + 1] = False         # 2 * 1 のマスを置く        if (row + 1 < h) and not used[row + 1][col]:            used[row][col] = used[row + 1][col] = True            dominos.append((row, col, row + 1, col))            dfs(nr, nc)            dominos.pop()            used[row][col] = used[row + 1][col] = False     dfs(0, 0)    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 ↗