結果
問題 |
No.1345 Beautiful BINGO
|
ユーザー |
![]() |
提出日時 | 2025-06-12 20:57:18 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,591 bytes |
コンパイル時間 | 413 ms |
コンパイル使用メモリ | 81,920 KB |
実行使用メモリ | 127,840 KB |
最終ジャッジ日時 | 2025-06-12 21:01:00 |
合計ジャッジ時間 | 4,580 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 5 TLE * 1 -- * 55 |
ソースコード
import sys import heapq from itertools import combinations def main(): input = sys.stdin.read().split() ptr = 0 N = int(input[ptr]) ptr +=1 M = int(input[ptr]) ptr +=1 grid = [] for i in range(N): row = list(map(int, input[ptr:ptr+N])) ptr +=N grid.append(row) lines = [] for i in range(N): # rows mask = 0 total = 0 for j in range(N): cell = i * N + j mask |= (1 << cell) total += grid[i][j] lines.append((mask, total)) for j in range(N): # columns mask = 0 total = 0 for i in range(N): cell = i * N + j mask |= (1 << cell) total += grid[i][j] lines.append((mask, total)) # main diagonals mask = 0 total =0 for i in range(N): j = i cell = i * N + j mask |= (1 << cell) total += grid[i][j] lines.append((mask, total)) mask =0 total =0 for i in range(N): j = N -1 -i cell = i * N + j mask |= (1 << cell) total += grid[i][j] lines.append((mask, total)) L = len(lines) heap = [] visited = dict() for idx in range(L): mask, cost = lines[idx] key = (1, mask) if key not in visited or cost < visited[key]: visited[key] = cost heapq.heappush(heap, (cost, 1, mask, [idx])) while heap: current_cost, count, current_mask, used = heapq.heappop(heap) if count >= M: print(current_cost) return if (count, current_mask) in visited and visited[(count, current_mask)] < current_cost: continue for idx in range(L): if idx in used: continue l_mask, l_cost = lines[idx] new_mask = current_mask | l_mask added = l_mask & (~current_mask) new_cost = current_cost + bin(added).count('1') * 0 sum_added = sum(grid[i][j] for i in range(N) for j in range(N) if (new_mask & (1 << (i*N + j))) and not (current_mask & (1 << (i*N + j)))) new_cost = current_cost + sum_added new_count = count + 1 new_used = used + [idx] key = (new_count, new_mask) if key in visited: if visited[key] <= new_cost: continue visited[key] = new_cost heapq.heappush(heap, (new_cost, new_count, new_mask, new_used)) print(-1) if __name__ == "__main__": main()