#K9071. Minimum Cost Path in a Grid

    ID: 37813 Type: Default 1000ms 256MiB

Minimum Cost Path in a Grid

Minimum Cost Path in a Grid

You are given a grid of dimensions \(n \times m\) where each cell has a positive integer representing its elevation. You need to find the minimum cost to travel from a starting cell \((sx, sy)\) to a target cell \((tx, ty)\) moving only in the four cardinal directions (up, down, left, right). The cost of moving from a cell with elevation \(a\) to an adjacent cell with elevation \(b\) is \(|a - b|\). When the starting and target cells are the same, the cost is \(0\).

Use Dijkstra's algorithm or any shortest path algorithm to solve this problem.

inputFormat

The input is read from stdin and has the following format:

  • The first line contains two integers \(n\) and \(m\): the number of rows and columns of the grid.
  • The next \(n\) lines each contain \(m\) space-separated integers, representing the grid elevations.
  • The last line contains four space-separated integers: \(sx\), \(sy\), \(tx\), \(ty\), representing the starting cell and the target cell (1-indexed).

outputFormat

Output to stdout a single integer which is the minimum cost required to travel from the starting cell to the target cell.

## sample
3 3
1 2 2
3 8 2
5 3 5
1 1 3 3
4