結果

問題 No.5006 Hidden Maze
ユーザー netyo715
提出日時 2022-06-12 18:04:21
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,075 bytes
コンパイル時間 518 ms
実行使用メモリ 113,352 KB
スコア 40,719
平均クエリ数 123.33
最終ジャッジ日時 2022-06-12 18:06:55
合計ジャッジ時間 152,501 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 58 TLE * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heapify, heappop as pop, heappush as push
from random import sample, randint
D = ((-1, 0), (0, -1), (0, 1), (1, 0))
def solve(H, W, P):
    stop_cnt = [[0]*(H*W) for _ in range(H*W)]
    is_empty = [[False]*(H*W) for _ in range(H*W)]
    def rc2idx(row, col):
        return row*W+col
    def idx2rc(idx):
        return idx//W, idx%W
    def is_empty_idx(idx1, idx2):
        return is_empty[idx1][idx2]
    def is_empty_rc(row1, col1, row2, col2):
        return is_empty_idx(rc2idx(row1, col1), rc2idx(row2, col2))
    def empty_per_idx(idx1, idx2):
        return 0
    def empty_per_rc(row1, col1, row2, col2):
        return empty_per_idx(rc2idx(row1, col1), rc2idx(row2, col2))
    def through_per_idx(idx1, idx2):
        if is_empty_idx(idx1, idx2):
            return (100-P)/100
        if stop_cnt[idx1][idx2]==1 and randint(1, 100)<=3:
            return (100-P)/100
        return (pow(P, stop_cnt[idx1][idx2])/pow(100, stop_cnt[idx1][idx2]-1)-P)/100

    def through_per_rc(row1, col1, row2, col2):
        return through_per_idx(rc2idx(row1, col1), rc2idx(row2, col2))
    def cmd_rc(row1, col1, row2, col2):
        if row1 > row2:
            return "U"
        if row1 < row2:
            return "D"
        if col1 > col2:
            return "L"
        if col1 < col2:
            return "R"
    def cmd_idx(idx1, idx2):
        r1, c1 = idx2rc(idx1)
        r2, c2 = idx2rc(idx2)
        return cmd_rc(r1, c1, r2, c2)

    for try_cnt in range(1, 1000+1):
        q = [(-1, 0, 0)]
        fixed = [[False]*W for _ in range(H)]
        dist = [[10]*W for _ in range(H)]
        prev = [[None]*W for _ in range(H)]
        while q:
            qper, qh, qw = pop(q)
            if fixed[qh][qw]:
                continue
            fixed[qh][qw] = True
            for a, b in sample(D, 4):
                h = qh+a; w = qw+b
                if not 0 <= h < H:
                    continue
                if not 0 <= w < W:
                    continue
                if fixed[h][w]:
                    continue
                per = -(-qper*through_per_rc(qh, qw, h, w))
                if dist[h][w] <= per:
                    continue
                q.append((per, h, w))
                prev[h][w] = (qh, qw)
                dist[h][w] = per
        node_idxs = []
        h, w = H-1, W-1
        while h+w:
            node_idxs.append(rc2idx(h, w))
            h, w = prev[h][w]
        node_idxs.append(0)
        node_idxs.reverse()
        cmd = []
        for i in range(len(node_idxs)-1):
            cmd.append(cmd_idx(node_idxs[i], node_idxs[i+1]))
        
        print(*cmd, sep="")
        through_cnt = int(input())
        if through_cnt == -1:
            return
        for i in range(through_cnt):
            is_empty[node_idxs[i]][node_idxs[i+1]] = True
            is_empty[node_idxs[i+1]][node_idxs[i]] = True
        stop_cnt[node_idxs[through_cnt]][node_idxs[through_cnt+1]] += 1
        stop_cnt[node_idxs[through_cnt+1]][node_idxs[through_cnt]] += 1

H, W, P = map(int, input().split())
solve(H, W, P)
0