提出 #22891558
ソースコード 拡げる
# local test max score: 946037519
from heapq import heappush, heappop
inf=10**18
move = {"U":(-1,0), "R":(0,1), "D":(1,0), "L":(0,-1)}
def dijkstra(d,p,sy,sx,pos_dir,loop):
hq = [(0, (sy,sx))] # (distance, node)
seen = [[False]*W for _ in range(H)]
while hq:
y,x = heappop(hq)[1]
seen[y][x] = True
for dir in move:
if loop < SWITCH_NUM*2 and dir not in pos_dir:
continue
ty, tx = y+move[dir][0], x+move[dir][1]
if not (0 <= ty <= H-1 and 0 <= tx <= W-1):
continue
cost = graph[ty][tx][dir]
if seen[ty][tx] == False and d[y][x] + cost < d[ty][tx]:
d[ty][tx] = d[y][x] + cost
heappush(hq, (d[ty][tx], (ty, tx)))
p[ty][tx] = [y,x]
def get_path(p,ty,tx):
path = []
total_cost = 0
while not (ty == -1 and tx == -1):
sy,sx = p[ty][tx]
dy = ty-sy
dx = tx-sx
tmp = [k for k, v in move.items() if v == (dy,dx)]
if tmp:
dir = tmp[0]
path.append(dir)
total_cost += graph[ty][tx][dir]
ty,tx = sy,sx
path.reverse()
return [path, total_cost]
def call_dijkstra(sy,sx,ty,tx,loop):
dist = [[inf]*W for _ in range(H)]
dist[sy][sx] = 0
prev =[[[-1,-1]]*W for _ in range(H)]
possible_dir = calc_possible_dir(sy,sx,ty,tx)
dijkstra(dist,prev,sy,sx,possible_dir,loop)
l, total_cost = get_path(prev,ty,tx)
return [l,total_cost]
def calc_possible_dir(sy,sx,ty,tx):
dy = ty-sy
dx = tx-sx
if dy < 0 and dx == 0: #U
return ["U","R","L"]
elif dy < 0 and dx > 0: #UR
return ["U","R"]
elif dy == 0 and dx > 0: #R
return ["U","R","D"]
elif dy > 0 and dx > 0: #DR
return ["R","D"]
elif dy > 0 and dx == 0: #D
return ["R","D","L"]
elif dy > 0 and dx < 0: #DL
return ["D","L"]
elif dy == 0 and dx < 0: #L
return ["D","L","U"]
elif dy < 0 and dx < 0: #UL
return ["L","U"]
def reverse_dir(dir):
if dir == "U":
dir = "D"
elif dir == "R":
dir = "L"
elif dir == "D":
dir = "U"
elif dir == "L":
dir = "R"
return dir
def update_graph_weight_ini(actual_cost,path,ty,tx,loop):
weighted_cost = actual_cost//len(path)
for dir in path[::-1]:
sy = ty - move[dir][0]
sx = tx - move[dir][1]
graph[ty][tx][dir] = graph[sy][sx][reverse_dir(dir)] = weighted_cost
ty,tx = sy,sx
def update_graph_weight_last(actual_cost,tmp_total_cost,path,ty,tx,loop):
for dir in path[::-1]:
sy = ty - move[dir][0]
sx = tx - move[dir][1]
weighted_cost = actual_cost * graph[ty][tx][dir]//tmp_total_cost
graph[ty][tx][dir] = graph[sy][sx][reverse_dir(dir)] = (graph[ty][tx][dir] + weighted_cost)//2
ty,tx = sy,sx
H = W = 30
RECALC_NUM = 100
SWITCH_NUM = 100
dirs = ["U","R","D","L"]
# {direction:cost}
graph = [[{"U":1, "R":1, "D":1, "L":1} for _ in range(W)] for _ in range(H)]
s_t_list = []
avg_actual_cost = 0
for i in range(1000):
# input position of start and goal
sy,sx,ty,tx = map(int,input().split())
# calculate minimum path with dijkstra
l,tmp_total_cost = call_dijkstra(sy,sx,ty,tx,i)
path = "".join(l)
print(path, flush=True)
# update weights of the graph based on the given cost and the used path
actual_cost = int(input())
avg_actual_cost = actual_cost//len(path) if avg_actual_cost == 0 else (avg_actual_cost + actual_cost//len(path))//2
# replace all graph weights which have the initial value(1)
# with the average of actual costs of initial RECALC_NUM loops
if i == RECALC_NUM:
for y in range(H):
for x in range(W):
for dir in dirs:
graph[y][x][dir] = (graph[y][x][dir] + avg_actual_cost)//2 if graph[y][x][dir] != 1 else avg_actual_cost
# # use initial RECALC_NUM inputs for increasing the accuracy of graph weight parameters
# if i < RECALC_NUM:
# s_t_list.append(((sy,sx,ty,tx), actual_cost, tmp_total_cost))
# if i == RECALC_NUM*3:
# for j in range(RECALC_NUM):
# sy2,sx2,ty2,tx2 = s_t_list[j][0]
# l2,tmp_total_cost2 = call_dijkstra(sy2,sx2,ty2,tx2,501)
# # if abs(s_t_list[j][1] - tmp_total_cost2) < s_t_list[j][1]*0.2:
# # continue
# path2 = "".join(l2)
# update_graph_weight_ini(s_t_list[j][1],path2,ty2,tx2,i)
# # update_graph_weight_last(s_t_list[j][1],tmp_total_cost2,path2,ty2,tx2,i)
# change the paramter tuning process for the first SWITCH_NUM loops and after that
if i < SWITCH_NUM:
update_graph_weight_ini(actual_cost,path,ty,tx,i)
else:
update_graph_weight_last(actual_cost,tmp_total_cost,path,ty,tx,i)
提出情報
ジャッジ結果
| セット名 |
test_ALL |
| 得点 / 配点 |
92065496651 / 100000000000 |
| 結果 |
|
| セット名 |
テストケース |
| test_ALL |
test_0000.txt, test_0001.txt, test_0002.txt, test_0003.txt, test_0004.txt, test_0005.txt, test_0006.txt, test_0007.txt, test_0008.txt, test_0009.txt, test_0010.txt, test_0011.txt, test_0012.txt, test_0013.txt, test_0014.txt, test_0015.txt, test_0016.txt, test_0017.txt, test_0018.txt, test_0019.txt, test_0020.txt, test_0021.txt, test_0022.txt, test_0023.txt, test_0024.txt, test_0025.txt, test_0026.txt, test_0027.txt, test_0028.txt, test_0029.txt, test_0030.txt, test_0031.txt, test_0032.txt, test_0033.txt, test_0034.txt, test_0035.txt, test_0036.txt, test_0037.txt, test_0038.txt, test_0039.txt, test_0040.txt, test_0041.txt, test_0042.txt, test_0043.txt, test_0044.txt, test_0045.txt, test_0046.txt, test_0047.txt, test_0048.txt, test_0049.txt, test_0050.txt, test_0051.txt, test_0052.txt, test_0053.txt, test_0054.txt, test_0055.txt, test_0056.txt, test_0057.txt, test_0058.txt, test_0059.txt, test_0060.txt, test_0061.txt, test_0062.txt, test_0063.txt, test_0064.txt, test_0065.txt, test_0066.txt, test_0067.txt, test_0068.txt, test_0069.txt, test_0070.txt, test_0071.txt, test_0072.txt, test_0073.txt, test_0074.txt, test_0075.txt, test_0076.txt, test_0077.txt, test_0078.txt, test_0079.txt, test_0080.txt, test_0081.txt, test_0082.txt, test_0083.txt, test_0084.txt, test_0085.txt, test_0086.txt, test_0087.txt, test_0088.txt, test_0089.txt, test_0090.txt, test_0091.txt, test_0092.txt, test_0093.txt, test_0094.txt, test_0095.txt, test_0096.txt, test_0097.txt, test_0098.txt, test_0099.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| test_0000.txt |
AC |
1019 ms |
89628 KiB |
| test_0001.txt |
AC |
1022 ms |
88020 KiB |
| test_0002.txt |
AC |
1052 ms |
88568 KiB |
| test_0003.txt |
AC |
1047 ms |
88612 KiB |
| test_0004.txt |
AC |
1085 ms |
88624 KiB |
| test_0005.txt |
AC |
1020 ms |
89008 KiB |
| test_0006.txt |
AC |
1057 ms |
89120 KiB |
| test_0007.txt |
AC |
1082 ms |
90072 KiB |
| test_0008.txt |
AC |
1047 ms |
88692 KiB |
| test_0009.txt |
AC |
1048 ms |
88908 KiB |
| test_0010.txt |
AC |
1036 ms |
87820 KiB |
| test_0011.txt |
AC |
997 ms |
88484 KiB |
| test_0012.txt |
AC |
1009 ms |
87936 KiB |
| test_0013.txt |
AC |
1014 ms |
91036 KiB |
| test_0014.txt |
AC |
1060 ms |
88240 KiB |
| test_0015.txt |
AC |
1050 ms |
89320 KiB |
| test_0016.txt |
AC |
1061 ms |
90480 KiB |
| test_0017.txt |
AC |
1032 ms |
88536 KiB |
| test_0018.txt |
AC |
1157 ms |
89960 KiB |
| test_0019.txt |
AC |
1097 ms |
90520 KiB |
| test_0020.txt |
AC |
1065 ms |
89264 KiB |
| test_0021.txt |
AC |
993 ms |
87140 KiB |
| test_0022.txt |
AC |
1056 ms |
88424 KiB |
| test_0023.txt |
AC |
1004 ms |
88872 KiB |
| test_0024.txt |
AC |
1047 ms |
88888 KiB |
| test_0025.txt |
AC |
1052 ms |
88968 KiB |
| test_0026.txt |
AC |
1033 ms |
88372 KiB |
| test_0027.txt |
AC |
1037 ms |
88136 KiB |
| test_0028.txt |
AC |
1058 ms |
88940 KiB |
| test_0029.txt |
AC |
1093 ms |
87960 KiB |
| test_0030.txt |
AC |
1034 ms |
89756 KiB |
| test_0031.txt |
AC |
1044 ms |
88600 KiB |
| test_0032.txt |
AC |
1081 ms |
90652 KiB |
| test_0033.txt |
AC |
1056 ms |
90960 KiB |
| test_0034.txt |
AC |
1015 ms |
88304 KiB |
| test_0035.txt |
AC |
1023 ms |
88424 KiB |
| test_0036.txt |
AC |
1020 ms |
88884 KiB |
| test_0037.txt |
AC |
999 ms |
88872 KiB |
| test_0038.txt |
AC |
1046 ms |
89140 KiB |
| test_0039.txt |
AC |
1043 ms |
88376 KiB |
| test_0040.txt |
AC |
1023 ms |
88160 KiB |
| test_0041.txt |
AC |
1043 ms |
87828 KiB |
| test_0042.txt |
AC |
1051 ms |
88892 KiB |
| test_0043.txt |
AC |
1053 ms |
89716 KiB |
| test_0044.txt |
AC |
1085 ms |
88696 KiB |
| test_0045.txt |
AC |
1032 ms |
88492 KiB |
| test_0046.txt |
AC |
1071 ms |
88808 KiB |
| test_0047.txt |
AC |
1046 ms |
88200 KiB |
| test_0048.txt |
AC |
1050 ms |
89020 KiB |
| test_0049.txt |
AC |
1014 ms |
88328 KiB |
| test_0050.txt |
AC |
1050 ms |
87892 KiB |
| test_0051.txt |
AC |
973 ms |
87696 KiB |
| test_0052.txt |
AC |
1090 ms |
88608 KiB |
| test_0053.txt |
AC |
1055 ms |
88524 KiB |
| test_0054.txt |
AC |
1054 ms |
89940 KiB |
| test_0055.txt |
AC |
1012 ms |
87812 KiB |
| test_0056.txt |
AC |
1048 ms |
88632 KiB |
| test_0057.txt |
AC |
1078 ms |
91172 KiB |
| test_0058.txt |
AC |
1063 ms |
88392 KiB |
| test_0059.txt |
AC |
1026 ms |
88860 KiB |
| test_0060.txt |
AC |
1129 ms |
89684 KiB |
| test_0061.txt |
AC |
1028 ms |
87636 KiB |
| test_0062.txt |
AC |
1070 ms |
88640 KiB |
| test_0063.txt |
AC |
1100 ms |
88872 KiB |
| test_0064.txt |
AC |
1046 ms |
88672 KiB |
| test_0065.txt |
AC |
1039 ms |
89324 KiB |
| test_0066.txt |
AC |
1090 ms |
88372 KiB |
| test_0067.txt |
AC |
1015 ms |
88444 KiB |
| test_0068.txt |
AC |
1067 ms |
90392 KiB |
| test_0069.txt |
AC |
1024 ms |
88016 KiB |
| test_0070.txt |
AC |
1025 ms |
90100 KiB |
| test_0071.txt |
AC |
993 ms |
88352 KiB |
| test_0072.txt |
AC |
989 ms |
89328 KiB |
| test_0073.txt |
AC |
1026 ms |
89696 KiB |
| test_0074.txt |
AC |
1064 ms |
87608 KiB |
| test_0075.txt |
AC |
1013 ms |
88116 KiB |
| test_0076.txt |
AC |
1046 ms |
89512 KiB |
| test_0077.txt |
AC |
1011 ms |
89436 KiB |
| test_0078.txt |
AC |
1042 ms |
89876 KiB |
| test_0079.txt |
AC |
1043 ms |
88576 KiB |
| test_0080.txt |
AC |
1045 ms |
89128 KiB |
| test_0081.txt |
AC |
1025 ms |
89156 KiB |
| test_0082.txt |
AC |
1042 ms |
89936 KiB |
| test_0083.txt |
AC |
1066 ms |
90676 KiB |
| test_0084.txt |
AC |
1032 ms |
89240 KiB |
| test_0085.txt |
AC |
1050 ms |
87832 KiB |
| test_0086.txt |
AC |
1005 ms |
87668 KiB |
| test_0087.txt |
AC |
1014 ms |
87668 KiB |
| test_0088.txt |
AC |
1044 ms |
88576 KiB |
| test_0089.txt |
AC |
1056 ms |
88848 KiB |
| test_0090.txt |
AC |
1041 ms |
89228 KiB |
| test_0091.txt |
AC |
1016 ms |
87912 KiB |
| test_0092.txt |
AC |
1005 ms |
88720 KiB |
| test_0093.txt |
AC |
1019 ms |
89136 KiB |
| test_0094.txt |
AC |
1082 ms |
89252 KiB |
| test_0095.txt |
AC |
1060 ms |
89908 KiB |
| test_0096.txt |
AC |
1085 ms |
90608 KiB |
| test_0097.txt |
AC |
1041 ms |
89276 KiB |
| test_0098.txt |
AC |
1052 ms |
90056 KiB |
| test_0099.txt |
AC |
1022 ms |
88196 KiB |