結果

問題 No.506 限られたジャパリまん
ユーザー qib
提出日時 2022-12-24 17:06:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 368 ms / 2,000 ms
コード長 905 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 77,184 KB
最終ジャッジ日時 2024-06-28 04:29:15
合計ジャッジ時間 5,181 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10 ** 9 + 7

h, w, k, p = map(int, input().split())
points = []
friends = []
for _ in range(k):
  x, y, name = input().split()
  points.append((int(x), int(y)))
  friends.append(name)

dp = [[-1 for _ in range(w + 1)] for _ in range(h + 1)]
s = None
for mask in range(1 << k):
  ndp = [[0 for _ in range(w + 1)] for _ in range(h + 1)]
  ndp[0][0] = 1
  banned = {}
  for i in range(k):
    if (mask >> i) & 1 != 0:
      continue
    x, y = points[i]
    banned[(x, y)] = True

  if len(banned) != k - p:
    continue

  for x in range(h + 1):
    for y in range(w + 1):
      if banned.get((x, y), False):
        continue
      if x > 0:
        ndp[x][y] += ndp[x - 1][y]
      if y > 0:
        ndp[x][y] += ndp[x][y - 1]

  if dp[-1][-1] < ndp[-1][-1]:
    dp = ndp
    s = mask

print(dp[-1][-1] % MOD)
if dp[-1][-1] != 0:
  for i in range(k):
    if (s >> i) & 1 != 0:
      print(friends[i])
0