문제 · 알고리즘 어려움 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 ms · $0.0021 ✓ count_tilings(['..', '..'])기댓값 2 1 ms ✓ count_tilings(['...', '...'])기댓값 3 <1 ms ✓ count_tilings(['.#', '..'])기댓값 0 <1 ms ✓ <1 ms✓ 1 ms✓ 1 ms✓ <1 ms✓ 2 ms✓ 2 ms✓ 1 ms✓ 2 ms✓ 1 ms⏱ 87 ms⏱ 4 ms⏱ 39 ms코드
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 ms · $0.0002 ✓ count_tilings(['..', '..'])기댓값 2 <1 ms ✓ count_tilings(['...', '...'])기댓값 3 <1 ms ✓ count_tilings(['.#', '..'])기댓값 0 <1 ms ✓ <1 ms✓ <1 ms✓ 2 ms✓ 2 ms✓ 5 ms✓ 3 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ 126 ms⏱ 4 ms⏱ 49 ms코드
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응답에 코드 없음 — ✗ 응답에 코드 없음✗ 응답에 코드 없음✗ 응답에 코드 없음✗ 응답에 코드 없음✗ 응답에 코드 없음✗ 응답에 코드 없음✗ 응답에 코드 없음✗ 응답에 코드 없음✗ 응답에 코드 없음⏱ 응답에 코드 없음⏱ 응답에 코드 없음⏱ 응답에 코드 없음
토큰 비용: $0.0023 · 진행 방식 및 점수 산정 안내 →