結果

問題 No.424 立体迷路
ユーザー takakintakakin
提出日時 2020-05-07 17:29:11
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 967 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 10,852 KB
実行使用メモリ 8,204 KB
最終ジャッジ日時 2023-09-16 08:01:37
合計ジャッジ時間 1,866 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,036 KB
testcase_01 AC 17 ms
8,140 KB
testcase_02 AC 16 ms
8,084 KB
testcase_03 AC 17 ms
8,192 KB
testcase_04 AC 17 ms
8,060 KB
testcase_05 AC 17 ms
8,100 KB
testcase_06 AC 16 ms
8,056 KB
testcase_07 AC 17 ms
8,196 KB
testcase_08 AC 16 ms
8,048 KB
testcase_09 AC 16 ms
8,116 KB
testcase_10 AC 17 ms
8,124 KB
testcase_11 AC 17 ms
8,204 KB
testcase_12 AC 16 ms
8,128 KB
testcase_13 AC 17 ms
8,116 KB
testcase_14 AC 18 ms
8,192 KB
testcase_15 AC 17 ms
8,076 KB
testcase_16 AC 17 ms
8,048 KB
testcase_17 AC 35 ms
8,032 KB
testcase_18 AC 35 ms
8,084 KB
testcase_19 AC 35 ms
8,116 KB
testcase_20 AC 35 ms
8,084 KB
testcase_21 AC 17 ms
8,120 KB
testcase_22 AC 17 ms
8,104 KB
testcase_23 AC 17 ms
8,128 KB
testcase_24 AC 42 ms
8,128 KB
testcase_25 AC 44 ms
8,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=lambda: sys.stdin.readline().rstrip()
h,w=map(int,input().split())
sx,sy,gx,gy=map(int,input().split())
B=[input() for _ in range(h)]
n=h*w
d=((1,0),(0,1),(-1,0),(0,-1))

par=[-1]*n

def find(x):
  if par[x]<0:
    return x
  else:
    par[x]=find(par[x])
    return par[x]

def unite(x,y):
  x=find(x)
  y=find(y)
  if x==y:
    return False
  else:
    if par[x]>par[y]:
      x,y=y,x
    par[x]+=par[y]
    par[y]=x
    return True

def same(x,y):
  return find(x)==find(y)

def size(x):
  return -par[find(x)]

for i in range(h):
  for j in range(w):
    cur=int(B[i][j])
    for dx,dy in d:
      if 0<=i+dx<h and 0<=j+dy<w:
        if abs(cur-int(B[i+dx][j+dy]))<=1:
          unite(i*w+j,(i+dx)*w+j+dy)
    for dx,dy in d:
      if 0<=i+2*dx<h and 0<=j+2*dy<w:
        if cur==int(B[i+2*dx][j+2*dy]) and int(B[i+dx][j+dy])<cur:
          unite(i*w+j,(i+2*dx)*w+j+2*dy)


if same((sx-1)*w+sy-1,(gx-1)*w+gy-1):
  print("YES")
else:
  print("NO")
0