結果
| 問題 | No.5006 Hidden Maze |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-06-12 16:01:34 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 733 ms / 2,000 ms |
| コード長 | 4,658 bytes |
| コンパイル時間 | 262 ms |
| 実行使用メモリ | 103,120 KB |
| スコア | 0 |
| 平均クエリ数 | 1000.00 |
| 最終ジャッジ日時 | 2022-06-12 16:06:36 |
| 合計ジャッジ時間 | 73,164 ms |
|
ジャッジサーバーID (参考情報) |
judge11 / judge10 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 100 |
ソースコード
from collections import defaultdict, deque
import sys
input = sys.stdin.readline
def iinput(): return int(input())
def sinput(): return input().rstrip()
def i0input(): return int(input()) - 1
def linput(): return list(input().split())
def liinput(): return list(map(int, input().split()))
def miinput(): return map(int, input().split())
def li0input(): return list(map(lambda x: int(x) - 1, input().split()))
def mi0input(): return map(lambda x: int(x) - 1, input().split())
INF = 10**20
MOD = 1000000007
from random import random, seed
seed(101010)
class UnionFindTree:
def __init__(self, initial_size:int) -> None:
self.n_nodes = initial_size
self.parents = [i for i in range(initial_size)]
self.ranks = [0 for i in range(initial_size)]
self.size = [1 for i in range(initial_size)]
self.n_roots = initial_size
def root(self, n:int) -> int:
if self.parents[n] == n:
return n
else:
self.parents[n] = self.root(self.parents[n])
return self.parents[n]
def same(self, n:int, m:int) -> bool:
return (self.root(n) == self.root(m))
def unite(self, n:int, m:int) -> None:
if self.same(n, m):
return
self.n_roots -= 1
n, m = self.root(n), self.root(m)
if self.ranks[n] < self.ranks[m]:
self.parents[n] = m
self.size[m] += self.size[n]
else:
self.parents[m] = n
self.size[n] += self.size[m]
if self.ranks[n] == self.ranks[m]:
self.ranks[n] += 1
def get_roots(self) -> set:
return set([self.root(x) for x in range(self.n_nodes)])
def count_roots(self) -> int:
return self.n_roots
def get_tree_size(self, n:int) -> int:
return self.size[self.root(n)]
def random_direction(p):
for i in range(3):
p[i+1] += p[i]
r = random()
for i in range(4):
if p[i] > r:
return i
H, W, p = liinput()
# URDL
probability = [[[0.25] * W for _ in [0] * H], [[0.75] * W for _ in [0] * H], [[0.25] * W for _ in [0] * H], [[0.50] * W for _ in [0] * H]]
p /= 100
direction_inv = {'U': 0, 'R': 1, 'D': 2, 'L': 3}
direction = 'URDL'
direction_coord = [(-1, 0), (0, 1), (1, 0), (0, -1)]
uf = UnionFindTree(H * W)
for i in range(W):
probability[0][0][i] = 0
probability[2][-1][i] = 0
for i in range(H):
probability[1][i][-1] = 0
probability[3][i][0] = 0
for _ in [0] * 1000:
place = [0, 0]
res = []
for _ in [0] * 400:
prob_tmp = []
for i in range(4):
try:
prob_tmp.append(probability[i][place[0]][place[1]])
except IndexError:
print('IndexError', place)
sum_prob = sum(prob_tmp)
for i in range(4):
prob_tmp[i] /= sum_prob
d = random_direction(prob_tmp)
dr, dc = direction_coord[d]
place[0] += dr
place[1] += dc
res.append(direction[d])
print(''.join(res), flush=True)
thr = iinput()
if thr == -1:
exit()
place = [0, 0]
for i in range(thr):
d = direction_inv[res[i]]
probability[d][place[0]][place[1]] = 1
dr, dc = direction_coord[d]
uf.unite(place[0] * W + place[1], (place[0] + dr) * W + place[1] + dc)
place[0] += dr
place[1] += dc
probability[(d+2)%4][place[0]][place[1]] = 1
if len(res) > thr:
d = direction_inv[res[thr]]
q = probability[d][place[0]][place[1]]
if q < 1:
new_p = (p * q) / (p * q + 1 - q)
probability[d][place[0]][place[1]] = new_p
dr, dc = direction_coord[d]
place[0] += dr
place[1] += dc
probability[(d+2)%4][place[0]][place[1]] = new_p
if uf.same(0, H*W-1):
seen = [[None] * W for _ in [0] * H]
seen[0][0] = ''
que = deque()
que.append((0, 0))
while que:
x, y = que.popleft()
for i in range(4):
dx, dy = direction_coord[i]
nx = x + dx
ny = y + dy
if nx < 0 or nx >= H or ny < 0 or ny >= W or not uf.same(x * W + y, nx * W + ny):
continue
if seen[nx][ny] is not None:
continue
seen[nx][ny] = seen[x][y] + direction[i]
que.append((nx, ny))
if nx == H-1 and ny == W-1:
print(seen[nx][ny], flush=True)
g = iinput()
if g == -1:
exit()
for s in seen:
print(s)