結果

問題 No.2838 Diagonals
ユーザー chineristAC
提出日時 2024-08-09 23:32:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 1,386 bytes
コンパイル時間 407 ms
コンパイル使用メモリ 82,372 KB
実行使用メモリ 86,052 KB
最終ジャッジ日時 2024-08-09 23:32:37
合計ジャッジ時間 3,058 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from itertools import permutations
from heapq import heappop,heappush
from collections import deque
import random
import bisect

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

mod = 998244353

def solve(N,M,S):
    S_cnt = 0
    E_cnt = 0
    for i in range(N):
        for j in range(M):
            if S[i][j] == "#":
                S_cnt += 1
                E_cnt += 2

                if i == 0 or S[i-1][j] == ".":
                    E_cnt += 1
                if j == 0 or S[i][j-1] == ".":
                    E_cnt += 1
    
    C_cnt = 0
    visit = [[False] * M for i in range(N)]
    for sx in range(N):
        for sy in range(M):
            if visit[sx][sy] or S[sx][sy] == ".":
                continue
            C_cnt += 1
            visit[sx][sy] = True
            st = [(sx,sy)]
            while st:
                x,y = st.pop()
                for dx,dy in [(0,1),(0,-1),(-1,0),(1,0)]:
                    nx,ny = x+dx,y+dy
                    if 0 <= nx < N and 0 <= ny < M:
                        if S[nx][ny] == "#" and not visit[nx][ny]:
                            st.append((nx,ny))
                            visit[nx][ny] = True
    
    #print(E_cnt)
    res = pow(2,E_cnt-S_cnt-C_cnt,mod)
    return res
    
N,M = mi()
S = [input() for i in range(N)]
print(solve(N,M,S))
0