#P4554. Minimum Cost Path on a Grid

    ID: 17800 Type: Default 1000ms 256MiB

Minimum Cost Path on a Grid

Minimum Cost Path on a Grid

In this problem, you are given an \(n \times m\) board consisting of two types of cells: # and @. You are also given a starting position and a target position. At each step, you can move one cell up, down, left, or right. The cost of moving to an adjacent cell is defined as follows:

  • If the cell you move to is of the same type as the current cell, the cost is \(0\).
  • If the cell you move to is of a different type, the cost is \(1\).

Your task is to compute the minimum cost needed to travel from the starting position to the target position.

Note: The positions are given as row and column indices (0-indexed).

inputFormat

The input consists of several lines:

  1. The first line contains two integers \(n\) and \(m\) denoting the number of rows and columns.
  2. The next \(n\) lines each contain a string of length \(m\) composed of characters '#' and '@', representing the board.
  3. The following line contains two integers: the starting row and starting column.
  4. The last line contains two integers: the target row and target column.

outputFormat

Output a single integer: the minimum cost to reach the target position from the start.

sample

3 3
#@#
@@@
#@#
0 0
2 2
2