結果

問題 No.1434 Make Maze
ユーザー tamatotamato
提出日時 2021-03-19 22:53:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 4,446 bytes
コンパイル時間 194 ms
コンパイル使用メモリ 82,648 KB
実行使用メモリ 88,860 KB
最終ジャッジ日時 2024-11-24 21:39:16
合計ジャッジ時間 6,750 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,180 KB
testcase_01 AC 35 ms
53,700 KB
testcase_02 AC 225 ms
88,832 KB
testcase_03 AC 218 ms
88,860 KB
testcase_04 AC 38 ms
53,764 KB
testcase_05 AC 38 ms
53,948 KB
testcase_06 AC 39 ms
53,576 KB
testcase_07 AC 36 ms
54,488 KB
testcase_08 AC 39 ms
54,208 KB
testcase_09 AC 36 ms
54,884 KB
testcase_10 AC 38 ms
53,948 KB
testcase_11 AC 37 ms
54,908 KB
testcase_12 AC 82 ms
76,704 KB
testcase_13 AC 43 ms
60,360 KB
testcase_14 AC 90 ms
76,512 KB
testcase_15 AC 120 ms
80,840 KB
testcase_16 AC 91 ms
77,152 KB
testcase_17 AC 85 ms
76,932 KB
testcase_18 AC 102 ms
77,692 KB
testcase_19 AC 163 ms
83,100 KB
testcase_20 AC 43 ms
62,152 KB
testcase_21 AC 46 ms
68,360 KB
testcase_22 AC 45 ms
66,580 KB
testcase_23 AC 38 ms
59,624 KB
testcase_24 AC 129 ms
81,624 KB
testcase_25 AC 171 ms
84,876 KB
testcase_26 AC 94 ms
77,016 KB
testcase_27 AC 137 ms
80,992 KB
testcase_28 AC 93 ms
77,188 KB
testcase_29 AC 75 ms
76,212 KB
testcase_30 AC 112 ms
78,108 KB
testcase_31 AC 106 ms
78,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10 ** -9


def main():
    import sys
    input = sys.stdin.readline

    class UnionFind():
        def __init__(self, n):
            self.n = n
            self.root = [-1] * (n + 1)
            self.rnk = [0] * (n + 1)

        def find_root(self, x):
            while self.root[x] >= 0:
                x = self.root[x]
            return x

        def unite(self, x, y):
            x = self.find_root(x)
            y = self.find_root(y)
            if x == y:
                return
            elif self.rnk[x] > self.rnk[y]:
                self.root[x] += self.root[y]
                self.root[y] = x
            else:
                self.root[y] += self.root[x]
                self.root[x] = y
                if self.rnk[x] == self.rnk[y]:
                    self.rnk[y] += 1

        def isSameGroup(self, x, y):
            return self.find_root(x) == self.find_root(y)

        def size(self, x):
            return -self.root[self.find_root(x)]

    def fin():
        print(-1)
        exit()

    def unite(h1, w1, h2, w2):
        if w1 == w2:
            grid[(h1 + h2) // 2][w1] = "."
        else:
            grid[h1][(w1 + w2) // 2] = "."

    def cut(h1, w1, h2, w2):
        if w1 == w2:
            grid[(h1 + h2) // 2][w1] = "#"
        else:
            grid[h1][(w1 + w2) // 2] = "#"

    H, W, X = map(int, input().split())
    grid = [["#"] * W for _ in range(H)]
    for h in range(H):
        for w in range(W):
            if h%2 == 0 and w%2 == 0:
                grid[h][w] = "."

    if X & 1:
        fin()
    H2 = H // 2 + 1
    W2 = W // 2 + 1
    X_max = (H2 * W2 - 1) * 2
    if H2 % 2 == 0 and W2 % 2 == 0:
        if X > X_max - 2:
            fin()
    else:
        if X > X_max:
            fin()
    if X < (H2 - 1 + W2 - 1) * 2:
        fin()

    if ((H2 - 1) + (W2 - 1))%2 == 0:
        if (X // 2) & 1:
            fin()
    else:
        if (X // 2) % 2 == 0:
            fin()

    for w in range(W-2):
        if w%2 == 0:
            unite(0, w, 0, w+2)
            X -= 2
    for h in range(H-2):
        if h%2 == 0:
            unite(h, W-1, h+2, W-1)
            X -= 2


    if H2 & 1:
        ww = W-1
        hh = 2
        while X:
            X -= 4
            unite(hh, ww, hh, ww-2)
            unite(hh+2, ww, hh+2, ww-2)
            unite(hh, ww-2, hh+2, ww-2)
            cut(hh, ww, hh+2, ww)
            ww -= 2
            if ww == 0:
                ww = W-1
                hh += 4
    elif W2 & 1:
        hh = 0
        ww = 0
        while X:
            X -= 4
            unite(hh, ww, hh+2, ww)
            unite(hh, ww+2, hh + 2, ww + 2)
            unite(hh+2, ww, hh+2, ww+2)
            cut(hh, ww, hh, ww+2)
            hh += 2
            if hh == H-1:
                hh = 0
                ww += 4
    else:
        ww = W - 1
        hh = 2
        while X:
            X -= 4
            unite(hh, ww, hh, ww - 2)
            unite(hh + 2, ww, hh + 2, ww - 2)
            unite(hh, ww-2, hh+2, ww-2)
            cut(hh, ww, hh + 2, ww)
            ww -= 2
            if ww == 0:
                ww = W - 1
                hh += 4
            if hh == H-1:
                break

        ww = 0
        while X:
            X -= 4
            unite(H-3, ww, H-1, ww)
            unite(H-3, ww+2, H-1, ww+2)
            unite(H-1, ww, H-1, ww+2)
            cut(H-3, ww, H-3, ww+2)
            ww += 4

    UF = UnionFind(H2 * W2)
    for hh in range(H2):
        for ww in range(W2):
            hw = hh * W2 + ww + 1

            hw_new_h = hw + W2
            if hh != H2-1:
                if grid[hh * 2 + 1][ww * 2] == ".":
                    UF.unite(hw, hw_new_h)
            hw_new_w = hw + 1
            if ww != W2-1:
                if grid[hh * 2][ww * 2 + 1] == ".":
                    UF.unite(hw, hw_new_w)
    for hh in range(H2):
        for ww in range(W2):
            hw = hh * W2 + ww + 1

            hw_new_h = hw + W2
            if hh != H2 - 1:
                if not UF.isSameGroup(hw, hw_new_h):
                    grid[hh * 2 + 1][ww * 2] = "."
                    UF.unite(hw, hw_new_h)
            hw_new_w = hw + 1
            if ww != W2 - 1:
                if not UF.isSameGroup(hw, hw_new_w):
                    grid[hh * 2][ww * 2 + 1] = "."
                    UF.unite(hw, hw_new_w)

    for h in range(H):
        print("".join(grid[h]))


if __name__ == '__main__':
    main()
0