给定一个 n * m 的矩阵 a,从左上角开始每次只能向右或者向下走,最后到达右下角的位置,路径上所有的数字累加起来就是路径和,输出所有的路径中最小的路径和。
public class Finder{
public static int[][] route;
public static int heigh;
public static int with;
static {
route = new int[][] {
{1, 2, 3}, {3, 4, 5}, {5, 6, 7}};
heigh = 2;
with = 2;
}
public static int getShortRoute(int[][] map, int x, int y) {
// 如果到达最后一个元素
if(x == with && y == heigh) {
return map[x][y];
}
// 如果y已经贴墙
else if( x < with && y == heigh) {
return map[x][y] + getShortRoute(map, x+1, y);
}