← 返回 databricks 的题目列表Fastest Transportation Mode in a Grid (Time-first, Cost-tiebreak)
类型:online_judge
Problem: Choose the Fastest Transportation Mode from S to D in a Grid (Time-first, Cost-tiebreak)
You are given a 2D grid grid representing a city. Each traversable cell is labeled as:
1..K: the transportation mode allowed on that cell (e.g., 1=Walk, 2=Bike, 3=Car, 4=Train).
X: blocked cell, cannot enter.
S: start.
D: destination.
You are also given two arrays of length K:
time[i]: minutes per block when using mode i+1.
cost[i]: dollars per block when using mode i+1.
You must choose exactly one transportation mode m and use it for the entire route from S to D.
Rules:
You may move only up/down/left/right (no diagonals).
You may enter any non-X cell.
If mode m is chosen, every numeric cell on the path must equal m (meaning that mode is available there). S and D are always enterable.
Objective:
Minimize total travel time.
Break ties by minimizing total travel cost.
Ignore modes that cannot reach D.
Return the name of the best mode (e.g., "Bike").
Suggested Input Format
First line: R C
Next R lines: C space-separated tokens (1..K/X/S/D)
Next line: K integers time[1..K]
Next line: K integers cost[1..K]
Output Format
One line: the best mode name
Constraints (suggested)
1 <= R, C <= 200
1 <= K <= 10
Example
Grid:
3 1 1 2 X 2
3 1 1 2 2 2
3 1 1 1 D 3
3 3 3 3 3 4
4 4 4 4 4 4
Legend: X=block, S=start, D=destination, 1=Walk,2=Bike,3=Car,4=Train
Time (min/block):
3 2 1 1
Cost ($/block):
0 1 3 2
Output:
Bike
Example
Input
5 6
3 1 1 2 X 2
3 1 1 2 2 2
3 1 1 1 D 3
3 3 3 3 3 4
4 4 4 4 4 4
3 2 1 1
0 1 3 2
Output
Bike