結果

問題 No.1434 Make Maze
ユーザー chineristACchineristAC
提出日時 2021-01-10 16:36:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 8,897 bytes
コンパイル時間 776 ms
コンパイル使用メモリ 86,928 KB
実行使用メモリ 95,256 KB
最終ジャッジ日時 2023-08-16 09:23:08
合計ジャッジ時間 9,485 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,200 KB
testcase_01 AC 74 ms
71,284 KB
testcase_02 AC 198 ms
95,256 KB
testcase_03 AC 196 ms
95,220 KB
testcase_04 AC 76 ms
71,404 KB
testcase_05 AC 75 ms
71,332 KB
testcase_06 AC 76 ms
71,000 KB
testcase_07 AC 74 ms
71,020 KB
testcase_08 AC 76 ms
71,192 KB
testcase_09 AC 76 ms
71,380 KB
testcase_10 AC 77 ms
71,312 KB
testcase_11 AC 75 ms
71,280 KB
testcase_12 AC 110 ms
77,592 KB
testcase_13 AC 77 ms
71,268 KB
testcase_14 AC 113 ms
77,692 KB
testcase_15 AC 133 ms
80,848 KB
testcase_16 AC 115 ms
78,000 KB
testcase_17 AC 110 ms
77,808 KB
testcase_18 AC 124 ms
79,960 KB
testcase_19 AC 151 ms
85,376 KB
testcase_20 AC 73 ms
71,192 KB
testcase_21 AC 75 ms
71,428 KB
testcase_22 AC 73 ms
71,460 KB
testcase_23 AC 76 ms
71,328 KB
testcase_24 AC 155 ms
85,720 KB
testcase_25 AC 176 ms
92,788 KB
testcase_26 AC 119 ms
78,332 KB
testcase_27 AC 137 ms
85,644 KB
testcase_28 AC 118 ms
78,356 KB
testcase_29 AC 96 ms
76,532 KB
testcase_30 AC 124 ms
80,060 KB
testcase_31 AC 131 ms
79,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

H,W,X = map(int,input().split())
h = (H+1)//2
w = (W+1)//2

if not 2*h+2*w-4<=X<=2*h*w-2:
    exit(print(-1))
diff = X - (2*h+2*w-4)
if diff%4!=0:
    exit(print(-1))

if w%2==1:
    ans = [["." for j in range(W)] for i in range(H)]
    pos = (0,0)
    rest_dist = X
    ans[0][0] = "T"
    while pos!=(H-1,W-1):
        x,y = pos
        if rest_dist != (H-1-x) + (W-1-y):
            if x==0:
                if (y//2)%2==1:
                    ans[x][y+1] = "T"
                    ans[x][y+2] = "T"
                    pos = (x,y+2)
                else:
                    ans[x+1][y] = "T"
                    ans[x+2][y] = "T"
                    pos = (x+2,y)
            elif x==H-1:
                if (y//2)%2==0:
                    ans[x][y+1] = "T"
                    ans[x][y+2] = "T"
                    pos = (x,y+2)
                else:
                    ans[x-1][y] = "T"
                    ans[x-2][y] = "T"
                    pos = (x-2,y)
            elif (y//2)%2==0:
                ans[x+1][y] = "T"
                ans[x+2][y] = "T"
                pos = (x+2,y)
            else:
                ans[x-1][y] = "T"
                ans[x-2][y] = "T"
                pos = (x-2,y)
            rest_dist -= 2
        else:
            while pos[1]!=W-1:
                x,y = pos
                ans[x][y+1] = "T"
                ans[x][y+2] = "T"
                pos = (x,y+2)
            while pos[0]!=H-1:
                x,y = pos
                ans[x+1][y] = "T"
                ans[x+2][y] = "T"
                pos = (x+2,y)


    stack = [(i,j) for i in range(H) for j in range(W) if ans[i][j]=="T" and i%2==0 and j%2==0]

    while stack:
        x,y = stack.pop()
        if x+2<=H-1 and ans[x+2][y]!="T":
            ans[x+2][y] = "T"
            ans[x+1][y] = "T"
            stack.append((x+2,y))
        if 2<=x and ans[x-2][y]!="T":
            ans[x-2][y] = "T"
            ans[x-1][y] = "T"
            stack.append((x-2,y))
        if y+2<=W-1 and ans[x][y+2]!="T":
            ans[x][y+2] = "T"
            ans[x][y+1] = "T"
            stack.append((x,y+2))
        if 2<=y and ans[x][y-2]!="T":
            ans[x][y-2] = "T"
            ans[x][y-1] = "T"
            stack.append((x,y-2))
    for i in range(H):
        for j in range(W):
            if ans[i][j]=="T":
                ans[i][j]="."
            else:
                ans[i][j]="#"
        print("".join(ans[i]))
elif h%2==1:
    H,W = W,H
    h,w = w,h
    ans = [["." for j in range(W)] for i in range(H)]
    pos = (0,0)
    rest_dist = X
    ans[0][0] = "T"
    while pos!=(H-1,W-1):
        x,y = pos
        if rest_dist != (H-1-x) + (W-1-y):
            if x==0:
                if (y//2)%2==1:
                    ans[x][y+1] = "T"
                    ans[x][y+2] = "T"
                    pos = (x,y+2)
                else:
                    ans[x+1][y] = "T"
                    ans[x+2][y] = "T"
                    pos = (x+2,y)
            elif x==H-1:
                if (y//2)%2==0:
                    ans[x][y+1] = "T"
                    ans[x][y+2] = "T"
                    pos = (x,y+2)
                else:
                    ans[x-1][y] = "T"
                    ans[x-2][y] = "T"
                    pos = (x-2,y)
            elif (y//2)%2==0:
                ans[x+1][y] = "T"
                ans[x+2][y] = "T"
                pos = (x+2,y)
            else:
                ans[x-1][y] = "T"
                ans[x-2][y] = "T"
                pos = (x-2,y)
            rest_dist -= 2
        else:
            while pos[1]!=W-1:
                x,y = pos
                ans[x][y+1] = "T"
                ans[x][y+2] = "T"
                pos = (x,y+2)
            while pos[0]!=H-1:
                x,y = pos
                ans[x+1][y] = "T"
                ans[x+2][y] = "T"
                pos = (x+2,y)


    stack = [(i,j) for i in range(H) for j in range(W) if ans[i][j]=="T" and i%2==0 and j%2==0]

    while stack:
        x,y = stack.pop()
        if x+2<=H-1 and ans[x+2][y]!="T":
            ans[x+2][y] = "T"
            ans[x+1][y] = "T"
            stack.append((x+2,y))
        if 2<=x and ans[x-2][y]!="T":
            ans[x-2][y] = "T"
            ans[x-1][y] = "T"
            stack.append((x-2,y))
        if y+2<=W-1 and ans[x][y+2]!="T":
            ans[x][y+2] = "T"
            ans[x][y+1] = "T"
            stack.append((x,y+2))
        if 2<=y and ans[x][y-2]!="T":
            ans[x][y-2] = "T"
            ans[x][y-1] = "T"
            stack.append((x,y-2))
    for i in range(H):
        for j in range(W):
            if ans[i][j]=="T":
                ans[i][j]="."
            else:
                ans[i][j]="#"

    rev = [[ans[j][i] for j in range(H)] for i in range(W)]
    for i in range(W):
        print("".join(rev[i]))
else:
    ans = [["." for j in range(W)] for i in range(H)]
    pos = (0,0)
    rest_dist = X
    ans[0][0] = "T"
    while pos!=(H-1,W-1):
        x,y = pos
        if rest_dist != (H-1-x) + (W-1-y):
            if y==W-3:
                if (x//2)%2==0:
                    ans[x][y+1] = "T"
                    ans[x][y+2] = "T"
                    pos = (x,y+2)
                else:
                    ans[x+1][y] = "T"
                    ans[x+2][y] = "T"
                    pos = (x+2,y)
            elif y==W-1:
                if (x//2)%2==0:
                    ans[x+1][y] = "T"
                    ans[x+2][y] = "T"
                    pos = (x+2,y)
                else:
                    ans[x][y-1] = "T"
                    ans[x][y-2] = "T"
                    pos = (x,y-2)
            else:
                if x==0:
                    if (y//2)%2==1:
                        ans[x][y+1] = "T"
                        ans[x][y+2] = "T"
                        pos = (x,y+2)
                    else:
                        ans[x+1][y] = "T"
                        ans[x+2][y] = "T"
                        pos = (x+2,y)
                elif x==H-1:
                    if (y//2)%2==0:
                        ans[x][y+1] = "T"
                        ans[x][y+2] = "T"
                        pos = (x,y+2)
                    else:
                        ans[x-1][y] = "T"
                        ans[x-2][y] = "T"
                        pos = (x-2,y)
                elif (y//2)%2==0:
                    ans[x+1][y] = "T"
                    ans[x+2][y] = "T"
                    pos = (x+2,y)
                else:
                    ans[x-1][y] = "T"
                    ans[x-2][y] = "T"
                    pos = (x-2,y)
            rest_dist -= 2
        else:
            if y==W-3:
                if (x//2)%2==0:
                    while pos[0]!=H-1:
                        x,y = pos
                        ans[x+1][y] = "T"
                        ans[x+2][y] = "T"
                        pos = (x+2,y)
                    while pos[1]!=W-1:
                        x,y = pos
                        ans[x][y+1] = "T"
                        ans[x][y+2] = "T"
                        pos = (x,y+2)
                else:
                    while pos[0]!=H-1:
                        x,y = pos
                        ans[x+1][y] = "T"
                        ans[x+2][y] = "T"
                        pos = (x+2,y)
                    while pos[1]!=W-1:
                        x,y = pos
                        ans[x][y+1] = "T"
                        ans[x][y+2] = "T"
                        pos = (x,y+2)
            elif y==W-1:
                while pos[1]!=W-1:
                    x,y = pos
                    ans[x][y+1] = "T"
                    ans[x][y+2] = "T"
                    pos = (x,y+2)
            else:
                while pos[1]!=W-1:
                    x,y = pos
                    ans[x][y+1] = "T"
                    ans[x][y+2] = "T"
                    pos = (x,y+2)
                while pos[0]!=H-1:
                    x,y = pos
                    ans[x+1][y] = "T"
                    ans[x+2][y] = "T"
                    pos = (x+2,y)

    stack = [(i,j) for i in range(H) for j in range(W) if ans[i][j]=="T" and i%2==0 and j%2==0]

    while stack:
        x,y = stack.pop()
        if x+2<=H-1 and ans[x+2][y]!="T":
            ans[x+2][y] = "T"
            ans[x+1][y] = "T"
            stack.append((x+2,y))
        if 2<=x and ans[x-2][y]!="T":
            ans[x-2][y] = "T"
            ans[x-1][y] = "T"
            stack.append((x-2,y))
        if y+2<=W-1 and ans[x][y+2]!="T":
            ans[x][y+2] = "T"
            ans[x][y+1] = "T"
            stack.append((x,y+2))
        if 2<=y and ans[x][y-2]!="T":
            ans[x][y-2] = "T"
            ans[x][y-1] = "T"
            stack.append((x,y-2))
    for i in range(H):
        for j in range(W):
            if ans[i][j]=="T":
                ans[i][j]="."
            else:
                ans[i][j]="#"
        print("".join(ans[i]))
0