第一题我的思路就是新建个二维arr数组,取arr[i+1][j]和arr[i][j+1]+arr2[i][j+2]的最小值, 可是只通过了 40% public class solution1 {     public static void main(String[] args) {         Scanner sc=new Scanner(System.in);         int n=sc.nextInt();         int[][] arr=new int[n][n];         for(int i=0;i<n;i++){             String s=sc.next();             String[] str=s.split(",");             int[] num=new int[n];             for(int k=0;k<n;k++){                 num[k]=Integer.valueOf(str[k]);             }             arr[i]=num;         }         int[][] arr2=new int[n][n];         for(int i=n-2;i>=0;i=i-2){             for(int j=n-1;j>=0;j--){                 if(j>=n-2){                     if(i==n-2){                         arr2[i][j]=arr[i+1][j];                     }else{                         arr2[i][j]=arr2[i+2][j]+arr[i+1][j];                     }                 }else{                     if(i==n-2){                         arr2[i][j]=Math.min(arr[i+1][j],arr[i][j+1]+arr2[i][j+2]);                     }else{                         arr2[i][j]=Math.min(arr[i+1][j]+arr2[i+2][j],arr[i][j+1]+arr2[i][j+2]);                     }                 }             }         }         int min=Integer.MAX_VALUE;         for(int i=0;i<n;i++){             min=Math.min(arr2[0][i],min);         }         System.out.println(min);     } }
点赞 评论

相关推荐

11-15 19:28
已编辑
蚌埠坦克学院 硬件开发
点赞 评论 收藏
分享
牛客网
牛客企业服务