Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created December 27, 2025 05:13
Show Gist options
  • Select an option

  • Save maehrm/bf298382a9c6ca9ddd720f9b4aaee300 to your computer and use it in GitHub Desktop.

Select an option

Save maehrm/bf298382a9c6ca9ddd720f9b4aaee300 to your computer and use it in GitHub Desktop.
import sys
sys.setrecursionlimit(10**9)
def dfs(i):
global ans, domino
if i == H * W:
score = 0
for y in range(H):
for x in range(W):
if not domino[y * W + x]:
score ^= A[y][x]
ans = max(ans, score)
return
# ドミノを置かない
dfs(i + 1)
y, x = divmod(i, W)
# ドミノを横に置く
if x + 1 < W and not domino[y * W + x] and not domino[y * W + (x + 1)]:
domino[y * W + x] = domino[y * W + (x + 1)] = True
dfs(i + 1)
domino[y * W + x] = domino[y * W + (x + 1)] = False
# ドミノを縦に置く
if y + 1 < H and not domino[y * W + x] and not domino[(y + 1) * W + x]:
domino[y * W + x] = domino[(y + 1) * W + x] = True
dfs(i + 1)
domino[y * W + x] = domino[(y + 1) * W + x] = False
return
H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
domino = [False] * (H * W)
ans = 0
dfs(0)
print(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment