E - 倉庫ロボットの配送 / Warehouse Robot Delivery 解説 by admin
Qwen3-Coder-480BOverview
A problem where a robot moves from start to goal via the shortest distance while passing through multiple specified cells. The order of visiting the waypoints is free, but all waypoints must be visited at least once.
Analysis
If we try to naively search all paths using BFS or similar approaches, the grid size \(N\) can be up to \(10^5\), making it impractical (computationally infeasible). Instead, we note that the number of waypoints \(M\) is at most 15, which is small.
Since the robot can only move up, down, left, and right, the shortest distance between two points can be computed as the Manhattan distance. Therefore, by precomputing the distances between important points (start, goal, waypoints), we can reduce the problem to optimizing the visiting order of waypoints.
This is a variation of the classic “Traveling Salesman Problem (TSP)” and can be solved efficiently using bit DP. By using bit DP, we manage which waypoints have been visited using bits while updating the minimum cost.
Algorithm
- Define a function
id_to_xythat converts each cell’s ID to grid coordinates \((r, c)\). - Define a function
manhattan_distancethat computes the Manhattan distance between any two points. - Consider a graph with start \(S\), goal \(T\), and \(M\) waypoints \(P_1, ..., P_M\) as nodes.
- Precompute the distances between all pairs of these nodes and create a distance table
dist_table. - Use bit DP to update the minimum cost with the state being the set of visited nodes (bitmask) and the current position:
dp[mask][i]:= the minimum cost when the set of nodes represented by bitmaskmaskhas been visited and the current position is nodei.
- The initial state is
dp[1][0] = 0(only the start has been visited). - In the bit DP transitions, exhaustively search which unvisited node to go to next and update the minimum cost.
- Finally, find the minimum cost to reach the goal after visiting all waypoints.
Complexity
- Time complexity: \(O(M^2 + M \cdot 2^M \cdot M) = O(M^2 \cdot 2^M)\)
- Space complexity: \(O(M^2 + M \cdot 2^M)\)
※ Since \(M \leq 15\), the exponential time from bit DP is well within the time limit.
Implementation Notes
Accurately convert cell numbers on the grid to \((r, c)\) coordinates.
Be careful not to make mistakes in the Manhattan distance calculation.
In the bit DP transitions, only select nodes that are not already included in
maskto proceed to the next state.The final answer is the minimum cost when “all waypoints have been visited” and “the last move is toward the goal.”
Source Code
import sys
from collections import deque
def id_to_xy(id, N):
id -= 1
return (id // N, id % N)
def manhattan_distance(id1, id2, N):
r1, c1 = id_to_xy(id1, N)
r2, c2 = id_to_xy(id2, N)
return abs(r1 - r2) + abs(c1 - c2)
def main():
import sys
input = sys.stdin.read
data = input().split()
N = int(data[0])
M = int(data[1])
S = int(data[2])
T = int(data[3])
P = list(map(int, data[4:4+M]))
# 頂点リスト:S, P1,...,PM, T
nodes = [S] + P + [T]
L = len(nodes)
# 距離テーブルを前計算
dist_table = [[0]*L for _ in range(L)]
for i in range(L):
for j in range(i+1, L):
d = manhattan_distance(nodes[i], nodes[j], N)
dist_table[i][j] = d
dist_table[j][i] = d
# dp[mask][i] := maskで表される頂点集合を訪問し、現在位置がnodes[i]である最小コスト
INF = float('inf')
dp = [[INF]*L for _ in range(1 << L)]
# 初期位置はS (nodes[0])
dp[1][0] = 0
# bit DP
for mask in range(1 << L):
for u in range(L):
if not (mask & (1 << u)):
continue
if dp[mask][u] == INF:
continue
current_cost = dp[mask][u]
for v in range(L):
if mask & (1 << v):
continue
new_mask = mask | (1 << v)
new_cost = current_cost + dist_table[u][v]
if new_cost < dp[new_mask][v]:
dp[new_mask][v] = new_cost
# 答え:すべての経由地(P)を通ってT(nodes[-1])に到達する最小コスト
# T以外の経由地は nodes[1] ~ nodes[M]
target_mask = (1 << (M+1)) - 1 # SとP1~PM
ans = INF
for i in range(1, L): # 最後はT (nodes[-1] = nodes[M+1])
if dp[target_mask][i] != INF:
ans = min(ans, dp[target_mask][i] + dist_table[i][L-1])
print(dp[(1 << L) - 1][L-1])
if __name__ == "__main__":
main()
This editorial was generated by qwen3-coder-480b.
投稿日時:
最終更新: