任务 · 算法 困难 Python ·
Domino tilings
The grid is a list of equal-length rows of "." (free cell) and "#" (blocked cell). Count the ways to cover every free cell with 1x2 dominoes placed horizontally or vertically, with no overlaps and no domino on a blocked cell. Return the count modulo 1,000,000,007. If there are no free cells, the answer is 1 (the empty tiling). Up to 400 rows and 1 to 8 columns.
函数签名
def count_tilings(grid: list[str]) -> int:示例
count_tilings(['..', '..']) # -> 2
count_tilings(['...', '...']) # -> 3
count_tilings(['.#', '..']) # -> 0另有 9 个隐藏测试和 3 个限时海量数据测试;其输入数据不公开。 任务以英文提示词提供给模型。
第 1 名Google Gemini 3.8 Flash已解决 · 通过 15/15 个测试生成耗时 3.3 秒 · 执行耗时 144 毫秒 · $0.0021 ✓ count_tilings(['..', '..'])预期 2 1 毫秒 ✓ count_tilings(['...', '...'])预期 3 <1 毫秒 ✓ count_tilings(['.#', '..'])预期 0 <1 毫秒 ✓ <1 毫秒✓ 1 毫秒✓ 1 毫秒✓ <1 毫秒✓ 2 毫秒✓ 2 毫秒✓ 1 毫秒✓ 2 毫秒✓ 1 毫秒⏱ 87 毫秒⏱ 4 毫秒⏱ 39 毫秒代码
def count_tilings(grid: list[str]) -> int: MOD = 1_000_000_007 if not grid or not grid[0]: return 1 R = len(grid) C = len(grid[0]) dp = {0: 1} for r in range(R): for c in range(C): cell_blocked = (grid[r][c] == '#') next_dp = {} for mask, count in dp.items(): is_filled = (mask >> c) & 1 if is_filled: # Current cell is already covered by a vertical domino from the cell above new_mask = mask ^ (1 << c) next_dp[new_mask] = (next_dp.get(new_mask, 0) + count) % MOD else: if cell_blocked: # Cell is blocked, leave it empty next_dp[mask] = (next_dp.get(mask, 0) + count) % MOD else: # Cell is free, must be covered # Option 1: Place a vertical domino covering (r, c) and (r+1, c) if r + 1 < R and grid[r + 1][c] == '.': new_mask = mask | (1 << c) next_dp[new_mask] = (next_dp.get(new_mask, 0) + count) % MOD # Option 2: Place a horizontal domino covering (r, c) and (r, c+1) if c + 1 < C and grid[r][c + 1] == '.' and not ((mask >> (c + 1)) & 1): new_mask = mask | (1 << (c + 1)) next_dp[new_mask] = (next_dp.get(new_mask, 0) + count) % MOD dp = next_dp if not dp: return 0 return dp.get(0, 0)第 2 名OpenAI GPT-6 Luna已解决 · 通过 15/15 个测试生成耗时 3.7 秒 · 执行耗时 194 毫秒 · $0.0002 ✓ count_tilings(['..', '..'])预期 2 <1 毫秒 ✓ count_tilings(['...', '...'])预期 3 <1 毫秒 ✓ count_tilings(['.#', '..'])预期 0 <1 毫秒 ✓ <1 毫秒✓ <1 毫秒✓ 2 毫秒✓ 2 毫秒✓ 5 毫秒✓ 3 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒⏱ 126 毫秒⏱ 4 毫秒⏱ 49 毫秒代码
def count_tilings(grid: list[str]) -> int: MOD = 1_000_000_007 n = len(grid) if n == 0: return 1 m = len(grid[0]) full = (1 << m) - 1 # Transpose when beneficial to minimize the profile width. if n < m: grid = [''.join(grid[r][c] for r in range(n)) for c in range(m)] n, m = m, n full = (1 << m) - 1 dp = {0: 1} for row in grid: blocked = 0 for c, ch in enumerate(row): if ch == '#': blocked |= 1 << c ndp = {} for incoming, ways in dp.items(): if incoming & blocked: continue occupied = incoming | blocked def fill(mask, outgoing): if mask == full: ndp[outgoing] = (ndp.get(outgoing, 0) + ways) % MOD return bit = (~mask & full) & -(~mask & full) c = bit.bit_length() - 1 # Place a horizontal domino. right = bit << 1 if c + 1 < m and not (mask & right): fill(mask | bit | right, outgoing) # Place a vertical domino into the next row. if not (blocked & bit): fill(mask | bit, outgoing | bit) fill(occupied, 0) dp = ndp return dp.get(0, 0)第 3 名MoonshotAI Kimi K2.7 Code未解决 · 通过 0/15 个测试生成耗时 120 秒 回复中无代码
✗ count_tilings(['..', '..'])预期 2回复中无代码 — ✗ count_tilings(['...', '...'])预期 3回复中无代码 — ✗ count_tilings(['.#', '..'])预期 0回复中无代码 — ✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码⏱ 回复中无代码⏱ 回复中无代码⏱ 回复中无代码第 3 名SpaceXAI Grok 4.7未解决 · 通过 0/15 个测试生成耗时 120 秒 回复中无代码
✗ count_tilings(['..', '..'])预期 2回复中无代码 — ✗ count_tilings(['...', '...'])预期 3回复中无代码 — ✗ count_tilings(['.#', '..'])预期 0回复中无代码 — ✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码✗ 回复中无代码⏱ 回复中无代码⏱ 回复中无代码⏱ 回复中无代码
Token 成本: $0.0023 · 轮次运行与评分规则 →