[編程題] 趕去公司

終于到周末啦!小易走在市區(qū)的街道上準(zhǔn)備找朋友聚會(huì),突然服務(wù)器發(fā)來警報(bào),小易需要立即回公司修復(fù)這個(gè)緊急bug。假設(shè)市區(qū)是一個(gè)無限大的區(qū)域,每條街道假設(shè)坐標(biāo)是(X,Y),小易當(dāng)前在(0,0)街道,辦公室在(gx,gy)街道上。小易周圍有多個(gè)出租車打車點(diǎn),小易趕去辦公室有兩種選擇,一種就是走路去公司,另外一種就是走到一個(gè)出租車打車點(diǎn),然后從打車點(diǎn)的位置坐出租車去公司。每次移動(dòng)到相鄰的街道(橫向或者縱向)走路將會(huì)花費(fèi)walkTime時(shí)間,打車將花費(fèi)taxiTime時(shí)間。小易需要盡快趕到公司去,現(xiàn)在小易想知道他最快需要花費(fèi)多少時(shí)間去公司。
輸入描述:
輸入數(shù)據(jù)包括五行:

第一行為周圍出租車打車點(diǎn)的個(gè)數(shù)n(1 ≤ n ≤ 50)

第二行為每個(gè)出租車打車點(diǎn)的橫坐標(biāo)tX[i] (-10000 ≤ tX[i] ≤ 10000)

第三行為每個(gè)出租車打車點(diǎn)的縱坐標(biāo)tY[i] (-10000 ≤ tY[i] ≤ 10000)

第四行為辦公室坐標(biāo)gx,gy(-10000 ≤ gx,gy ≤ 10000),以空格分隔

第五行為走路時(shí)間walkTime(1 ≤ walkTime ≤ 1000)和taxiTime(1 ≤ taxiTime ≤ 1000),以空格分隔

輸出描述:
輸出一個(gè)整數(shù)表示,小易最快能趕到辦公室的時(shí)間

輸入例子1:
2
-2 -2
0 -2
-4 -2
15 3

輸出例子1:
42

代碼

import java.util.*;
public class Main {
     public static void main(String[] args) {
          
        Scanner in = new Scanner(System.in);
          
        while(in.hasNext()) {
            int n = in.nextInt();
            int[] tx = new int[n];
            for(int i=0;i<n;i++) {
                tx[i] = in.nextInt();
            }
            int[] ty = new int[n];
            for(int i=0;i<n;i++) {
                ty[i] = in.nextInt();
            }
            int gx = in.nextInt();
            int gy = in.nextInt();
            int wt = in.nextInt();
            int tt = in.nextInt();
            System.out.println(solve(tx, ty, gx, gy,wt,tt,n));
        }
          
    }
      
    private static int solve(int[] tx, int[] ty, int gx, int gy, int wt, int tt, int n) {
         
        int walk = (Math.abs(gx) + Math.abs(gy)) * wt;
         
        int drive = Integer.MAX_VALUE;
         
        for(int i = 0;i<n;i++) {
            int cost = (Math.abs(tx[i]) + Math.abs(ty[i])) * wt + (Math.abs(tx[i]-gx) + Math.abs(ty[i]-gy)) * tt;
            drive = Math.min(drive, cost);
        }
         
        return Math.min(drive, walk);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容