結果

問題 No.2412 YOU Grow Bigger!
ユーザー mkawa2mkawa2
提出日時 2023-08-11 23:19:16
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,985 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 77,236 KB
最終ジャッジ日時 2024-04-29 14:29:56
合計ジャッジ時間 3,761 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
52,608 KB
testcase_01 AC 47 ms
52,608 KB
testcase_02 AC 46 ms
53,248 KB
testcase_03 AC 87 ms
76,160 KB
testcase_04 AC 94 ms
76,288 KB
testcase_05 AC 119 ms
76,804 KB
testcase_06 AC 113 ms
76,800 KB
testcase_07 AC 91 ms
76,288 KB
testcase_08 AC 117 ms
76,544 KB
testcase_09 AC 61 ms
64,768 KB
testcase_10 AC 54 ms
61,312 KB
testcase_11 AC 46 ms
53,888 KB
testcase_12 AC 44 ms
53,376 KB
testcase_13 AC 83 ms
73,728 KB
testcase_14 WA -
testcase_15 AC 66 ms
65,792 KB
testcase_16 WA -
testcase_17 AC 61 ms
64,000 KB
testcase_18 AC 104 ms
76,544 KB
testcase_19 AC 54 ms
61,184 KB
testcase_20 AC 46 ms
54,016 KB
testcase_21 AC 81 ms
73,088 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 119 ms
76,932 KB
testcase_26 AC 60 ms
64,256 KB
testcase_27 WA -
testcase_28 AC 105 ms
76,544 KB
testcase_29 WA -
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(1000005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

class UnionFind:
    def __init__(self, n):
        self._tree = [-1]*n
        # number of connected component
        self.cnt = n

    def root(self, u):
        stack = []
        while self._tree[u] >= 0:
            stack.append(u)
            u = self._tree[u]
        for v in stack: self._tree[v] = u
        return u

    def same(self, u, v):
        return self.root(u) == self.root(v)

    def merge(self, u, v):
        u, v = self.root(u), self.root(v)
        if u == v: return False
        if self._tree[u] > self._tree[v]: u, v = v, u
        self._tree[u] += self._tree[v]
        self._tree[v] = u
        self.cnt -= 1
        return True

    # size of connected component
    def size(self, u):
        return -self._tree[self.root(u)]

h, w = LI()
ss = [[(c == "#")*1 for c in SI()] for _ in range(h)]

for i in range(h):
    for j in range(w):
        if ss[i][j] == 1:
            for di, dj in dij:
                ni, nj = i+di, j+dj
                if ni < 0 or nj < 0 or ni >= h or nj >= w: continue
                if ss[ni][nj] == 0: ss[ni][nj] = 2
# p2D(ss)

s = h*w
t = s+1
uf = UnionFind(h*w+2)
i = 0
for j in range(w):
    if ss[i][j]: uf.merge(s, i*w+j)
j = w-1
for i in range(h):
    if ss[i][j]: uf.merge(s, i*w+j)
i = h-1
for j in range(w):
    if ss[i][j]: uf.merge(t, i*w+j)
j = 0
for i in range(h):
    if ss[i][j]: uf.merge(t, i*w+j)
for i in range(h):
    for j in range(w):
        if i+1 < h and ss[i][j] and ss[i+1][j]: uf.merge(i*w+j, (i+1)*w+j)
        if j+1 < w and ss[i][j] and ss[i][j+1]: uf.merge(i*w+j, i*w+j+1)

if uf.same(s, t):
    print(0)
    exit()

def ok(i, j):
    res = 0
    for di in range(-2, 3):
        for dj in range(-2, 3):
            if (di,dj)==(-2,-2) or (di,dj)==(2,-2) or (di,dj)==(-2,2) or (di,dj)==(2,2):continue
            ni, nj = i+di, j+dj
            if ni < 0 or nj < 0 or ni >= h or nj >= w: continue
            if uf.same(ni*w+nj, s): res |= 1
            if uf.same(ni*w+nj, t): res |= 2
            if res == 3: return True
    return False

for i in range(1, h-1):
    for j in range(1, w-1):
        if i < 3 and j < 3: continue
        if i >= h-3 and j >= w-3: continue
        if ok(i, j):
            print(1)
            exit()

print(2)
0