結果

問題 No.506 限られたジャパリまん
ユーザー gew1fw
提出日時 2025-06-12 20:27:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,497 bytes
コンパイル時間 342 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 78,288 KB
最終ジャッジ日時 2025-06-12 20:28:14
合計ジャッジ時間 12,975 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

H, W, K, P = map(int, input().split())
friends = []
for _ in range(K):
    x, y, name = input().split()
    friends.append((int(x), int(y), name))

max_count = 0
best_mask = 0

for mask in range(0, 1 << K):
    cnt = bin(mask).count('1')
    if cnt > P:
        continue
    
    blocked = set()
    for i in range(K):
        if not (mask & (1 << i)):
            x, y, _ = friends[i]
            blocked.add((x, y))
    
    dp = [[0] * (W + 1) for _ in range(H + 1)]
    if (0, 0) not in blocked:
        dp[0][0] = 1
    
    for x in range(H + 1):
        for y in range(W + 1):
            if x == 0 and y == 0:
                continue
            if (x, y) in blocked:
                dp[x][y] = 0
                continue
            total = 0
            if x > 0:
                total += dp[x-1][y]
            if y > 0:
                total += dp[x][y-1]
            dp[x][y] = total % MOD
    
    current = dp[H][W]
    
    if current > max_count:
        max_count = current
        best_mask = mask
    elif current == max_count and current != 0:
        current_included = [i for i in range(K) if (mask & (1 << i))]
        best_included = [i for i in range(K) if (best_mask & (1 << i))]
        if current_included < best_included:
            best_mask = mask

if max_count == 0:
    print(0)
else:
    print(max_count % MOD)
    included_names = [friends[i][2] for i in range(K) if (best_mask & (1 << i))]
    for name in included_names:
        print(name)
0