結果

問題 No.2838 Diagonals
ユーザー sasa8uyauya
提出日時 2024-11-07 12:43:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 237 ms / 2,000 ms
コード長 609 bytes
コンパイル時間 422 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 80,136 KB
最終ジャッジ日時 2024-11-07 12:43:24
合計ジャッジ時間 4,496 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

def root(x):
  p=x
  l=[p]
  while r[p]!=p:
    p=r[p]
    l+=[p]
  for p in l:
    r[p]=l[-1]
  return r[x]

def union(x,y):
  rx=root(x)
  ry=root(y)
  if rx==ry:
    return
  if rx>ry:
    rx,ry=ry,rx
  r[ry]=rx
  return

h,w=map(int,input().split())
s=[input() for i in range(h)]
r=list(range(h*w))
M=998244353
c=1
for i in range(h):
  for j in range(w):
    if s[i][j]=="#":
      c*=2 if i-1>=0 and j-1>=0 and root((i-1)*w+j)==root(i*w+(j-1)) else 4
      c%=M
      if i-1>=0 and s[i-1][j]=="#":
        union(i*w+j,(i-1)*w+j)
      if j-1>=0 and s[i][j-1]=="#":
        union(i*w+j,i*w+(j-1))
print(c)
0