Cross the street
Practice
4.2 (6 votes)
Algorithms
Approved
Dijkstra's algorithm
Easy
Graphs
Hiring
Recruit
Shortest path algorithm
Approved
Problem
76% Success 1688 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

ABC Company is involved in the logistics business.

The company has many outlets and stockyards in a city. The city is like an \(N \times M\) grid. We consider a single cell of the given grid to be a single block in the city. The stockyard is at the upper-left corner and the outlet is located in the lower right corner. Everyday, one of the employees has to travel from the upper left to the lower right corner for supplies. Each block in the city has a height, where the height of the block located at position (i,j) in the grid is equal to\(A[i][j]\). The company wants to change the heights of some of the blocks, so that the employee can enjoy a high-speed drive from the stockyard to the outlet. But this change comes at a certain cost.

Specifically, if they change a block height from x to y, then they must pay exactly \(|x-y|\) dollars. Please help them find the minimum cost, such that by spending that specific amount, they can get a path from stockyard to the outlet with all blocks along the path having the same height.

In a single move, the employee can move from a block to any of its adjacent blocks. Note that during this journey, the employee is allowed to move in all four directions, fulfilling the condition that he never goes out of the grid at any point in time.

Input :

First line contains two positive integers N and M - number of rows and columns in the city. Then, N lines follow, each containing M integers, where the \(j^{th}\) integer on the \(i^{th}\) line denotes \(A[i][j]\).

Output :

The first and only line of output should contain minimum cost.

Constraints :

1<= N, M <=100

1<= height of blocks <=100

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
14 votes
Tags:
AlgorithmsDijkstra's AlgorithmEasyGraphsShortest Path Algorithm
Points:30
41 votes
Tags:
EasyGraphsShortest Path Algorithm
Points:30
11 votes
Tags:
AlgorithmsDynamic ProgrammingFloyd Warshall AlgorithmGraphsShortest Path AlgorithmString Manipulation