← 返回 databricks 的题目列表Grid pathfinding with time & cost optimality (BFS / shortest path variant)
类型:online_judge
Given an m x n grid, you need to move from the top-left cell (0,0) to the bottom-right cell (m-1,n-1). Each move/cell contributes to two metrics:
time: total time to reach the target
cost: total monetary cost along the path
Design an algorithm to output the optimal result under lexicographic minimization: minimize time first; among all paths with the same minimum time, minimize cost (i.e., minimize the pair (time, cost)).
Requirements:
Moves are allowed in 4 directions (up/down/left/right).
Each cell affects time and/or cost as specified by the input.
Output two integers: the minimum time, and the minimum cost among paths achieving that minimum time.
Suggested constraints (typical interview setting):
1 <= m, n <= 200
all weights are non-negative integers
Example (illustrative): input is the grid weight specification (provided in the interview), output is min_time min_cost.
Note: The original post didn’t include the exact input format, so this is a standard, reproducible variant (solvable via Dijkstra / 0-1 BFS / multi-criteria shortest path).
Example
Input
2 2
0 0 1 5
2 1 0 0
Output
0 0