結果

問題 No.2412 YOU Grow Bigger!
ユーザー mkawa2mkawa2
提出日時 2023-08-11 23:37:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 161 ms / 6,000 ms
コード長 3,023 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 82,364 KB
実行使用メモリ 78,424 KB
最終ジャッジ日時 2024-05-01 06:38:26
合計ジャッジ時間 3,996 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
54,284 KB
testcase_01 AC 38 ms
54,040 KB
testcase_02 AC 39 ms
54,776 KB
testcase_03 AC 100 ms
76,504 KB
testcase_04 AC 117 ms
77,152 KB
testcase_05 AC 161 ms
78,424 KB
testcase_06 AC 134 ms
77,236 KB
testcase_07 AC 101 ms
76,596 KB
testcase_08 AC 136 ms
77,172 KB
testcase_09 AC 58 ms
69,324 KB
testcase_10 AC 48 ms
63,124 KB
testcase_11 AC 44 ms
60,560 KB
testcase_12 AC 39 ms
53,816 KB
testcase_13 AC 70 ms
74,136 KB
testcase_14 AC 54 ms
65,364 KB
testcase_15 AC 59 ms
68,212 KB
testcase_16 AC 58 ms
67,648 KB
testcase_17 AC 54 ms
66,080 KB
testcase_18 AC 112 ms
77,188 KB
testcase_19 AC 54 ms
63,104 KB
testcase_20 AC 42 ms
54,644 KB
testcase_21 AC 68 ms
73,656 KB
testcase_22 AC 77 ms
76,396 KB
testcase_23 AC 154 ms
77,320 KB
testcase_24 AC 149 ms
77,608 KB
testcase_25 AC 143 ms
77,644 KB
testcase_26 AC 52 ms
65,684 KB
testcase_27 AC 50 ms
63,972 KB
testcase_28 AC 82 ms
76,492 KB
testcase_29 AC 55 ms
66,108 KB
testcase_30 AC 38 ms
53,780 KB
権限があれば一括ダウンロードができます

ソースコード

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(1,w):
    ss[i][j]=1
    uf.merge(s, i*w+j)
j = w-1
for i in range(h-1):
    ss[i][j]=1
    uf.merge(s, i*w+j)
i = h-1
for j in range(w-1):
    ss[i][j]=1
    uf.merge(t, i*w+j)
j = 0
for i in range(1,h):
    ss[i][j]=1
    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
    if i < 2 or j >= w-2: res |= 1
    if i >= h-2 or j < 2: res |= 2
    for di in range(-2, 3):
        for dj in range(-2, 3):
            if abs(di) == abs(dj) == 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