結果
| 問題 | No.506 限られたジャパリまん | 
| コンテスト | |
| ユーザー |  lam6er | 
| 提出日時 | 2025-04-16 16:02:38 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,536 bytes | 
| コンパイル時間 | 454 ms | 
| コンパイル使用メモリ | 81,800 KB | 
| 実行使用メモリ | 77,036 KB | 
| 最終ジャッジ日時 | 2025-04-16 16:08:56 | 
| 合計ジャッジ時間 | 8,532 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 12 WA * 13 | 
ソースコード
H, W, K, P = map(int, input().split())
friends = []
grid = [[-1 for _ in range(W+1)] for _ in range(H+1)]
for idx in range(K):
    x, y, name = input().split()
    x = int(x)
    y = int(y)
    friends.append((x, y, name))
    grid[x][y] = idx
mod = 10**9 + 7
max_count = 0
best_mask = 0
for mask in range(0, 1 << K):
    cnt = bin(mask).count('1')
    if cnt > P:
        continue
    
    dp = [[0] * (W + 1) for _ in range(H + 1)]
    dp[0][0] = 1
    
    for s in range(0, H + W + 1):
        for i in range(0, H + 1):
            j = s - i
            if j < 0 or j > W:
                continue
            if i == 0 and j == 0:
                continue
            
            friend_idx = grid[i][j]
            blocked = False
            if friend_idx != -1:
                if not (mask & (1 << friend_idx)):
                    blocked = True
            
            if blocked:
                dp[i][j] = 0
                continue
            
            total = 0
            if i > 0:
                total += dp[i-1][j]
            if j > 0:
                total += dp[i][j-1]
            dp[i][j] = total % mod
    
    current_count = dp[H][W]
    
    if current_count > max_count or (current_count == max_count and mask < best_mask and current_count != 0):
        max_count = current_count
        best_mask = mask
if max_count == 0:
    print(0)
else:
    print(max_count % mod)
    selected = [friends[idx][2] for idx in range(K) if (best_mask & (1 << idx))]
    for name in selected:
        print(name)
            
            
            
        