結果

問題 No.506 限られたジャパリまん
ユーザー gew1fw
提出日時 2025-06-12 19:44:21
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,008 bytes
コンパイル時間 260 ms
コンパイル使用メモリ 82,912 KB
実行使用メモリ 77,520 KB
最終ジャッジ日時 2025-06-12 19:44:28
合計ジャッジ時間 6,825 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 2 WA * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

H, W, K, P = map(int, input().split())

friends = []
grid = [[None 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] = (x, y, name)

# Precompute friend_idx: for each cell, the index of the friend or -1
friend_idx = [[-1] * (W+1) for _ in range(H+1)]
for fi in range(K):
    x, y, name = friends[fi]
    friend_idx[x][y] = fi

# Precompute the number of paths for all possible subsets T of blocked friends
max_T = 1 << K
paths = [0] * max_T

for T in range(max_T):
    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 this cell is blocked
            blocked = False
            fi = friend_idx[i][j]
            if fi != -1:
                if (T & (1 << fi)) != 0:
                    blocked = True
            if blocked:
                dp[i][j] = 0
                continue
            # Compute the number of ways
            total = 0
            if i > 0:
                total += dp[i-1][j]
            if j > 0:
                total += dp[i][j-1]
            dp[i][j] = total % MOD
    paths[T] = dp[H][W]

# Now iterate through all possible subsets S of friends to give passes
max_paths = -1
best_S = None

for S in range(max_T):
    cnt = bin(S).count('1')
    if cnt > P:
        continue
    # T is the complement of S, the friends not in S are blocked
    T = ((1 << K) - 1) ^ S
    current_paths = paths[T]
    if current_paths == 0:
        continue
    if current_paths > max_paths:
        max_paths = current_paths
        best_S = S

if max_paths == -1 or max_paths == 0:
    print(0)
else:
    print(max_paths % MOD)
    selected = []
    for idx in range(K):
        if (best_S & (1 << idx)) != 0:
            selected.append(friends[idx][2])
    for name in selected:
        print(name)
    print()
0