Problem10631--Minimum Cost to reach the end of the matrix

10631: Minimum Cost to reach the end of the matrix

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 512 MiB

Description

Given a matrix of size $m \times n$, such that each cell has an Uppercase Character in it, find the minimum cost to reach the cell $(m, n)$ starting from $(1, 1)$. 
From a cell $(i, j)$, we can move Up (i-1, j), Down (i+1, j), Left (i, j-1) and Right (i, j+1). If the adjacent cell has the same character as the current cell then we can move to the adjacent cell with 0 cost, else the cost incurred is 1 coin.

Input

第一行给两个整数 $m,n\ (1 \leq m,n \leq 1000)$。
其后 $m$ 行,每行 $n$ 个小写字母。

Output

The minimum cost to reach the cell $(m, n)$ starting from $(1, 1)$. 

Sample 1 Input

2 3
abn
def

Sample 1 Output

3
One of the possible answers is to move from ‘a’ -> ‘b’ -> ‘e’ -> ‘f’ with the cost of 1 + 1 + 1 = 3

Sample 2 Input

2 2
aa
aa

Sample 2 Output

0
We can take any path from the top left to the bottom right as the cost is always zero.

Source/Category