結果

問題 No.506 限られたジャパリまん
ユーザー lam6er
提出日時 2025-03-31 17:31:57
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,636 bytes
コンパイル時間 314 ms
コンパイル使用メモリ 82,652 KB
実行使用メモリ 77,976 KB
最終ジャッジ日時 2025-03-31 17:33:04
合計ジャッジ時間 12,508 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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 = None

for mask in range(0, 1 << K):
    # Check if the number of set bits is <= P
    bit_count = bin(mask).count('1')
    if bit_count > P:
        continue
    
    # Generate blocked cells (friends not in this mask)
    blocked = set()
    for idx in range(K):
        if not (mask & (1 << idx)):
            xf, yf, _ = friends[idx]
            blocked.add((xf, yf))
    
    # Initialize DP table
    dp = [[0] * (W + 1) for _ in range(H + 1)]
    dp[0][0] = 1  # Starting point
    
    for i in range(H + 1):
        for j in range(W + 1):
            if i == 0 and j == 0:
                continue  # Already initialized
            if (i, j) in blocked:
                dp[i][j] = 0
                continue
            top = dp[i-1][j] if i > 0 else 0
            left = dp[i][j-1] if j > 0 else 0
            dp[i][j] = (top + left) % MOD
    
    current_count = dp[H][W]
    if current_count > max_count:
        max_count = current_count
        best_mask = mask
    elif current_count == max_count and current_count != 0:
        # According to problem statement, there is a unique maximum
        # So this case won't occur for valid test cases
        pass

if max_count == 0 or best_mask is None:
    print(0)
else:
    print(max_count % MOD)
    # Collect the names in the order of input
    selected_names = [friends[idx][2] for idx in range(K) if (best_mask & (1 << idx))]
    for name in selected_names:
        print(name)
0