結果

問題 No.506 限られたジャパリまん
ユーザー lam6er
提出日時 2025-04-15 23:37:27
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,536 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 81,964 KB
実行使用メモリ 76,412 KB
最終ジャッジ日時 2025-04-15 23:38:27
合計ジャッジ時間 7,551 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0