結果

問題 No.2412 YOU Grow Bigger!
ユーザー mkawa2mkawa2
提出日時 2023-08-11 23:26:54
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,041 bytes
コンパイル時間 759 ms
コンパイル使用メモリ 82,240 KB
実行使用メモリ 77,404 KB
最終ジャッジ日時 2024-11-18 18:55:15
合計ジャッジ時間 3,629 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,492 KB
testcase_01 AC 37 ms
54,088 KB
testcase_02 AC 42 ms
53,704 KB
testcase_03 AC 77 ms
76,336 KB
testcase_04 AC 81 ms
76,492 KB
testcase_05 AC 109 ms
76,624 KB
testcase_06 AC 102 ms
76,536 KB
testcase_07 AC 84 ms
76,500 KB
testcase_08 AC 108 ms
76,904 KB
testcase_09 AC 52 ms
65,080 KB
testcase_10 AC 47 ms
62,312 KB
testcase_11 AC 41 ms
54,580 KB
testcase_12 AC 38 ms
53,616 KB
testcase_13 AC 72 ms
74,516 KB
testcase_14 AC 54 ms
63,900 KB
testcase_15 AC 60 ms
65,756 KB
testcase_16 WA -
testcase_17 AC 54 ms
63,924 KB
testcase_18 AC 96 ms
76,680 KB
testcase_19 AC 45 ms
62,460 KB
testcase_20 AC 40 ms
54,652 KB
testcase_21 AC 72 ms
73,740 KB
testcase_22 WA -
testcase_23 AC 128 ms
77,124 KB
testcase_24 WA -
testcase_25 AC 110 ms
77,116 KB
testcase_26 AC 51 ms
64,612 KB
testcase_27 AC 106 ms
76,616 KB
testcase_28 AC 87 ms
76,700 KB
testcase_29 WA -
testcase_30 AC 38 ms
55,408 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(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
    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 (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