結果

問題 No.13 囲みたい!
ユーザー zimphazimpha
提出日時 2017-12-07 21:11:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 73 ms / 5,000 ms
コード長 988 bytes
コンパイル時間 160 ms
コンパイル使用メモリ 82,108 KB
実行使用メモリ 77,352 KB
最終ジャッジ日時 2024-05-06 19:28:18
合計ジャッジ時間 1,638 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,260 KB
testcase_01 AC 36 ms
53,428 KB
testcase_02 AC 39 ms
55,960 KB
testcase_03 AC 57 ms
71,280 KB
testcase_04 AC 43 ms
64,392 KB
testcase_05 AC 73 ms
77,352 KB
testcase_06 AC 54 ms
69,704 KB
testcase_07 AC 68 ms
77,276 KB
testcase_08 AC 57 ms
72,844 KB
testcase_09 AC 57 ms
67,768 KB
testcase_10 AC 52 ms
67,876 KB
testcase_11 AC 63 ms
72,240 KB
testcase_12 AC 45 ms
62,040 KB
testcase_13 AC 57 ms
70,048 KB
testcase_14 AC 64 ms
73,908 KB
testcase_15 AC 38 ms
56,908 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