結果
| 問題 | No.506 限られたジャパリまん | 
| コンテスト | |
| ユーザー |  gew1fw | 
| 提出日時 | 2025-06-12 14:44:50 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,623 bytes | 
| コンパイル時間 | 380 ms | 
| コンパイル使用メモリ | 82,472 KB | 
| 実行使用メモリ | 77,124 KB | 
| 最終ジャッジ日時 | 2025-06-12 14:46:16 | 
| 合計ジャッジ時間 | 5,975 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 2 WA * 23 | 
ソースコード
MOD = 10**9 + 7
H, W, K, P = map(int, input().split())
friends = []
for _ in range(K):
    x, y, name = input().split()
    x = int(x)
    y = int(y)
    friends.append((x, y, name))
# Precompute the grid: each cell has the friend index or -1
grid = [[-1 for _ in range(W + 1)] for __ in range(H + 1)]
for idx in range(K):
    x, y, name = friends[idx]
    grid[x][y] = idx
max_paths = 0
best_mask = None
for mask in range(0, 1 << K):
    s = bin(mask).count('1')
    if s > P:
        continue
    
    # Initialize DP table
    dp = [[0] * (W + 1) for _ in range(H + 1)]
    dp[0][0] = 1
    
    for i in range(H + 1):
        for j in range(W + 1):
            if i == 0 and j == 0:
                continue
            
            # Check if the cell is blocked
            if grid[i][j] != -1:
                friend_idx = grid[i][j]
                if (mask & (1 << friend_idx)) == 0:
                    dp[i][j] = 0
                    continue
            
            # Calculate the number of paths
            sum_val = 0
            if i > 0:
                sum_val += dp[i-1][j]
            if j > 0:
                sum_val += dp[i][j-1]
            dp[i][j] = sum_val % MOD
    
    current = dp[H][W]
    if current > max_paths:
        max_paths = current
        best_mask = mask
if max_paths == 0:
    print(0)
else:
    print(max_paths % MOD)
    # Collect the names in the order of their occurrence in the input
    output = []
    for idx in range(K):
        if (best_mask & (1 << idx)) != 0:
            output.append(friends[idx][2])
    for name in output:
        print(name)
    print()
            
            
            
        