結果

問題 No.1434 Make Maze
ユーザー tamatotamato
提出日時 2021-03-19 22:53:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 4,446 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 88,704 KB
最終ジャッジ日時 2024-05-03 18:11:21
合計ジャッジ時間 7,441 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,992 KB
testcase_01 AC 39 ms
52,864 KB
testcase_02 AC 252 ms
88,704 KB
testcase_03 AC 252 ms
88,192 KB
testcase_04 AC 40 ms
53,376 KB
testcase_05 AC 39 ms
52,992 KB
testcase_06 AC 40 ms
53,248 KB
testcase_07 AC 39 ms
53,248 KB
testcase_08 AC 40 ms
52,992 KB
testcase_09 AC 40 ms
53,504 KB
testcase_10 AC 40 ms
52,992 KB
testcase_11 AC 40 ms
53,120 KB
testcase_12 AC 90 ms
76,160 KB
testcase_13 AC 45 ms
59,776 KB
testcase_14 AC 100 ms
77,092 KB
testcase_15 AC 145 ms
80,068 KB
testcase_16 AC 109 ms
77,272 KB
testcase_17 AC 97 ms
77,004 KB
testcase_18 AC 119 ms
77,548 KB
testcase_19 AC 184 ms
83,328 KB
testcase_20 AC 46 ms
60,800 KB
testcase_21 AC 53 ms
67,328 KB
testcase_22 AC 52 ms
65,024 KB
testcase_23 AC 44 ms
59,520 KB
testcase_24 AC 160 ms
81,536 KB
testcase_25 AC 197 ms
84,892 KB
testcase_26 AC 104 ms
77,064 KB
testcase_27 AC 158 ms
80,640 KB
testcase_28 AC 105 ms
77,384 KB
testcase_29 AC 81 ms
76,288 KB
testcase_30 AC 119 ms
77,564 KB
testcase_31 AC 125 ms
78,380 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