โจทย์ · อัลกอริทึม ยาก 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 การทดสอบชุดข้อมูลขนาดใหญ่ ภายใต้เวลาจำกัด โดยไม่แสดงข้อมูลนำเข้า โจทย์ที่ส่งให้โมเดลเป็นภาษาอังกฤษ
อันดับ #1Google 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)อันดับ #2OpenAI 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)อันดับ #3MoonshotAI Kimi K2.7 Codeแก้ไม่ผ่าน · ผ่าน 0 จาก 15 การทดสอบตอบใน 120 วินาที ไม่มีโค้ดในคำตอบ
✗ count_tilings(['..', '..'])ค่าที่คาดหวัง 2ไม่มีโค้ดในคำตอบ — ✗ count_tilings(['...', '...'])ค่าที่คาดหวัง 3ไม่มีโค้ดในคำตอบ — ✗ count_tilings(['.#', '..'])ค่าที่คาดหวัง 0ไม่มีโค้ดในคำตอบ — ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ⏱ ไม่มีโค้ดในคำตอบ⏱ ไม่มีโค้ดในคำตอบ⏱ ไม่มีโค้ดในคำตอบอันดับ #3SpaceXAI Grok 4.7แก้ไม่ผ่าน · ผ่าน 0 จาก 15 การทดสอบตอบใน 120 วินาที ไม่มีโค้ดในคำตอบ
✗ count_tilings(['..', '..'])ค่าที่คาดหวัง 2ไม่มีโค้ดในคำตอบ — ✗ count_tilings(['...', '...'])ค่าที่คาดหวัง 3ไม่มีโค้ดในคำตอบ — ✗ count_tilings(['.#', '..'])ค่าที่คาดหวัง 0ไม่มีโค้ดในคำตอบ — ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ✗ ไม่มีโค้ดในคำตอบ⏱ ไม่มีโค้ดในคำตอบ⏱ ไม่มีโค้ดในคำตอบ⏱ ไม่มีโค้ดในคำตอบ
ค่าใช้จ่ายโทเค็น: $0.0023 · วิธีจัดการแข่งขันและการคิดคะแนน →