結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,992 KB
testcase_01 AC 40 ms
52,992 KB
testcase_02 AC 37 ms
53,120 KB
testcase_03 AC 71 ms
76,544 KB
testcase_04 AC 71 ms
76,544 KB
testcase_05 AC 98 ms
76,508 KB
testcase_06 AC 88 ms
76,928 KB
testcase_07 AC 72 ms
77,056 KB
testcase_08 AC 93 ms
76,800 KB
testcase_09 AC 50 ms
65,024 KB
testcase_10 AC 45 ms
61,312 KB
testcase_11 AC 39 ms
54,144 KB
testcase_12 AC 35 ms
52,992 KB
testcase_13 AC 63 ms
74,240 KB
testcase_14 WA -
testcase_15 AC 51 ms
65,920 KB
testcase_16 WA -
testcase_17 AC 49 ms
64,000 KB
testcase_18 AC 86 ms
76,544 KB
testcase_19 AC 44 ms
61,568 KB
testcase_20 AC 38 ms
53,760 KB
testcase_21 AC 62 ms
73,088 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 95 ms
76,672 KB
testcase_26 AC 47 ms
64,256 KB
testcase_27 WA -
testcase_28 AC 80 ms
76,620 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