Problem3015--小 X 学游泳(swim)

3015: 小 X 学游泳(swim)

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

Description

暑假快到啦,小 X 准备趁着这个暑假去学游泳。可是一开始小 X 就遇到了一个难题。

游泳池划分成了一个 lns="http://www.w3.org/1998/Math/MathML">n \times m 的方格, 这里 lns="http://www.w3.org/1998/Math/MathML">n \times m 表示 lns="http://www.w3.org/1998/Math/MathML">n 行 lns="http://www.w3.org/1998/Math/MathML">m 列。 因 为游泳池里的水深浅不一,所以这 lns="http://www.w3.org/1998/Math/MathML">n×m 个方格对于小 X 的危险系数也会不一样。

而小 X 目前需要从左上角 的方格 lns="http://www.w3.org/1998/Math/MathML">(1,1) 出发, 游到右下角 的方格 lns="http://www.w3.org/1998/Math/MathML">(n,m),小 X 每次只 能从当前方格游到上下左右四个相邻的方格中的某一格,并且在到达终点前不能离开游泳池。

小 X 很担心会发生什么危险,所以希望你能帮他找一条危险系数最小的路径。

一条路径的危险系数定义为这条路径所经过的方格的危险系数之和。

注意:这条路径不能经过同一个方格两次(小 X 当然不希望去那么危险的地方再游一次)

Input

输入数据第一行有两个用空格隔开的正整数 lns="http://www.w3.org/1998/Math/MathML">n 和 lns="http://www.w3.org/1998/Math/MathML">m, 表示泳池的行数和列数。

接下来共有 lns="http://www.w3.org/1998/Math/MathML">n 行数据,每行有 lns="http://www.w3.org/1998/Math/MathML">m 个用空格隔开的大于等于 lns="http://www.w3.org/1998/Math/MathML">0 的整数, 表示每个方格的危险系数

Output

输出仅有一行包含一个整数 lns="http://www.w3.org/1998/Math/MathML">ans , 表示要求的从左上角的方格 lns="http://www.w3.org/1998/Math/MathML">(1,1) 出发, 游到右下角的方格 lns="http://www.w3.org/1998/Math/MathML">(n,m) 的最小的危险系数。

Sample Input Copy

4 5
1 7 2 8 2
3 10 1 5 1
2 8 3 7 1
1 2 1 20 1

Sample Output Copy

19

HINT

【数据范围】

对于 lns="http://www.w3.org/1998/Math/MathML">30\% 的数据, lns="http://www.w3.org/1998/Math/MathML">1 ≤ n,m ≤ 5

对于另外 lns="http://www.w3.org/1998/Math/MathML">40\% 的数据, lns="http://www.w3.org/1998/Math/MathML">1 ≤ n,m ≤ 20 , 每个方格的危险系数 = lns="http://www.w3.org/1998/Math/MathML">0 或 lns="http://www.w3.org/1998/Math/MathML">1

对于 lns="http://www.w3.org/1998/Math/MathML">100\% 的数据, lns="http://www.w3.org/1998/Math/MathML">1 ≤ n,m ≤ 30, lns="http://www.w3.org/1998/Math/MathML">0 ≤ 每个方格的危险系数 lns="http://www.w3.org/1998/Math/MathML">≤ 100000

【样例解释】

路径: lns="http://www.w3.org/1998/Math/MathML">(1,1) → lns="http://www.w3.org/1998/Math/MathML">(1,2) → lns="http://www.w3.org/1998/Math/MathML">(1,3) → lns="http://www.w3.org/1998/Math/MathML">(2,3) → lns="http://www.w3.org/1998/Math/MathML">(2,4) → lns="http://www.w3.org/1998/Math/MathML">(2,5) → lns="http://www.w3.org/1998/Math/MathML">(3,5) → lns="http://www.w3.org/1998/Math/MathML">(4,5) , 危险系数之和为 lns="http://www.w3.org/1998/Math/MathML">1+7+2+1+5+1+1+1=19 。

Source/Category