結果

問題 No.13 囲みたい!
ユーザー zimphazimpha
提出日時 2017-12-07 21:11:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 95 ms / 5,000 ms
コード長 988 bytes
コンパイル時間 1,367 ms
コンパイル使用メモリ 86,736 KB
実行使用メモリ 78,668 KB
最終ジャッジ日時 2023-08-19 12:20:24
合計ジャッジ時間 2,906 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
71,044 KB
testcase_01 AC 65 ms
71,248 KB
testcase_02 AC 68 ms
71,196 KB
testcase_03 AC 83 ms
76,764 KB
testcase_04 AC 70 ms
76,388 KB
testcase_05 AC 95 ms
78,188 KB
testcase_06 AC 85 ms
76,860 KB
testcase_07 AC 93 ms
78,668 KB
testcase_08 AC 85 ms
76,828 KB
testcase_09 AC 77 ms
76,652 KB
testcase_10 AC 78 ms
76,516 KB
testcase_11 AC 86 ms
76,416 KB
testcase_12 AC 70 ms
75,424 KB
testcase_13 AC 81 ms
76,536 KB
testcase_14 AC 91 ms
77,820 KB
testcase_15 AC 68 ms
70,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve(h, w, cells, mapping):
  for i, c in enumerate(cells):
    mapping[c[0]][c[1]] = i
  n = len(cells)
  dsu = [i for i in range(n)]

  def root(x):
    if x != dsu[x]:
      dsu[x] = root(dsu[x])
    return dsu[x]

  dx = [0, 1]
  dy = [1, 0]
  for c in cells:
    for i in range(2):
      x, y = c[0] + dx[i], c[1] + dy[i]
      if x < 0 or x >= h or y < 0 or y >= w or mapping[x][y] == -1:
        continue
      u = root(mapping[c[0]][c[1]])
      v = root(mapping[x][y])
      if u != v:
        dsu[u] = v
      else:
        return True
  for i, c in enumerate(cells):
    mapping[c[0]][c[1]] = -1
  return False

w, h = list(map(int, input().split()))
cells = [[] for i in range(1000)]
mapping = [None] * h
for i in range(h):
  a = list(map(int, input().split()))
  mapping[i] = [-1] * w
  for j in range(w):
    cells[a[j] - 1].append((i, j))

flag = False
for e in cells:
  if solve(h, w, e, mapping):
    flag = True
    break
print('possible' if flag else 'impossible')
0