ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

美团笔试 从矩阵起点到终点的最小花费

2021-05-10 19:03:21  阅读:265  来源: 互联网

标签:tempfei arr int 美团 笔试 矩阵 fei && jilu


1.第一行输入m,n,k
表示m行n维的数组,k表示接下来有k行数据,如1 1 2 2 1,从(1,1)到(2,2)的费用为1,
求从起点到终点,即(1,1)到(5,4)的最小花费为多少
5 4 3
1 1 2 2 1
1 1 5 4 3
2 2 5 4 1



import java.util.*;
import  java.util.Scanner;
public class fei1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int k = in.nextInt();
        int[][] arr = new int[k][5];
        for (int i = 0; i < k; i++) {
            for (int j = 0; j < 5; j++) {
                arr[i][j] = in.nextInt();
            }
        }

        int tempi = 0;
        int tempj = 0;
        int fei = Integer.MAX_VALUE;
        int tempfei = Integer.MAX_VALUE;
        int temp_s = 0;
        int label = 0;
        int[] jilu = new int[k];
        for (int ti = 0; ti < k; ti++) {
            if(jilu[ti]==1)
            {
                continue;
            }
            label = 0;
            for (int i = 0; i < k; i++) {
                if(jilu[i]==1)
                {
                    continue;
                }


                if (arr[i][0] == 1 && arr[i][1] == 1 && label == 0)  {
                    if (arr[i][2] == n && arr[i][3] == m) {
                        tempfei = arr[i][4];
                        fei = Math.min(tempfei, fei);
                        jilu[i] =1;
                        break;
                    } else {


                        tempi = arr[i][2];
                        tempj = arr[i][3];
                        temp_s = arr[i][4];
                        tempfei = arr[i][4];
                        jilu[i] =1;
                        //break;


                    }
                    label = 1;
                } else if ((arr[i][0] == tempi && arr[i][1] == tempj) && (arr[i][2] != n || arr[i][3] != m)) {
                    tempi = arr[i][2];
                    tempj = arr[i][3];
                    temp_s = arr[i][4];
                    tempfei = tempfei + arr[i][4];
                    jilu[i] =1;
                } else if (arr[i][0] == tempi && arr[i][1] == tempj && arr[i][2] == n && arr[i][3] == m) {
                    tempfei = tempfei + arr[i][4];
                    fei = Math.min(tempfei, fei);
                    jilu[i] =1;
                    break;
                }
            }
        }


        if (fei == 0) {
            System.out.println(-1);
        }
        System.out.println(fei);


    }
}















标签:tempfei,arr,int,美团,笔试,矩阵,fei,&&,jilu
来源: https://blog.csdn.net/qq_37602161/article/details/116605720

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有