AcWing 1018. 最低通行费JAVA
原题链接
简单
作者:
ARM
,
2020-08-28 10:18:42
,
所有人可见
,
阅读 353
如果没有设定步数则就是迷宫问题
java 代码
import java.lang.*;
import java.util.*;
class Main{
static int n = 0, N = 110;
static int[][] nums = new int[N][N];
static int[][] min = new int[N][N];
static int getMin(){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(i - 1 == 0 && j - 1 == 0)min[i][j] = nums[i][j];
else if(i - 1 == 0){
min[i][j] = min[i][j - 1] + nums[i][j];
}else if(j - 1 == 0){
min[i][j] = min[i - 1][j] + nums[i][j];
}else{
min[i][j] = Math.min(min[i - 1][j], min[i][j - 1]) + nums[i][j];
}
}
}
return min[n][n];
}
public static void main(String[] args)throws Exception{
Scanner s = new Scanner(System.in);
n = s.nextInt();
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
nums[i][j] = s.nextInt();
}
}
int res = getMin();
System.out.print(res);
}
}