Lexicographic Grid Travel.
Unlock detailed company stats for this questionUpgrade
You are given an m × n grid with a start cell S and a destination cell D. Each cell contains a value from {0,1,2,3,4}, where 0 is blocked, and 1–4 correspond to bicycle, motorbike, car, and bus, respectively. You are also given matrices T and C representing the time and cost to enter each cell. You must choose exactly one mode of transport for the entire trip (no switching) and can move up, down, left, or right to adjacent cells compatible with your mode. Your goal is to find a path from S to D that minimizes (total_time, total_cost) lexicographically—that is, prefer smaller time, and among paths with equal time, smaller cost. Return the minimal (time, cost) across all modes, or -1 if the destination is unreachable.
Community answers
No answers contributed by the community yet.
Related courses

Course

Course
Course
