阿里在線筆試題


題目如下:

如下圖,某物流派送員p,需要給 a、b、c、d. 4個快遞點派送包裹,請問派送員需要選擇什么樣的路線,才能完成最短路程的派送。假設如圖派送員的起點坐標(0,0),派送路線只能沿着圖中的方格邊行駛,每個小格都是正方形,且邊長為1,如p到d的距離就是4。隨機輸入n個派送點坐標,求輸出最短派送路線值(從起點開始完成n個點派送並回到起始點的距離)。 

輸入示例: 

2,2 
2,8 
4,4 
7,2 
輸出: 
30

拿到這道題首先要分析題目,要求最短派送的路線,可以遍歷出所有路徑一一對比,也可以采用回溯算法。

 

 1 import java.util.Scanner;
 2 
 3 //定義一個坐標類
 4 class Point{
 5     //(x,y)坐標
 6     int x,y;
 7     //判斷是否遍歷過的標志變量
 8     boolean isVisited;
 9     
10     //構造函數
11     public Point(int x, int y) {
12         this.x = x;
13         this.y = y;
14         this.isVisited = false;//初始化為false,若遍歷過則置為true 
15     }
16     
17     //獲得該點到指定點的距離
18     public int getLength(Point p) {
19         return Math.abs(x-p.x) + Math.abs(y-p.y);
20     }
21 }
22 
23 //處理最短路徑的方法類
24 public class Method{
25     //起始點
26     static Point  StartPoint = new Point(0, 0);
27     //最小路徑先設為系統最大值
28     static int minPath = Integer.MAX_VALUE;
29     
30     /**
31      * 定義獲取給定點到其余點的最小路徑的方法,遞歸調用
32      * @param point        給定的起始點
33      * @param points    其余的點
34      * @param totalLen    起始點到遍歷點的距離和
35      * @param count        用來對遍歷過的點計數
36      * @return
37      */
38     public static int caculate(Point p, Point[] points, int totalLen, int count) {
39         //判斷停止條件,如果所有的點遍歷完,則返回
40         if(points.length==count) {
41             minPath = Math.min(minPath, totalLen + p.getLength(StartPoint));
42             return minPath;
43         }
44         
45         //否則遍歷其余的點並進行路徑和的計算
46         for(int i=0; i<points.length; i++) {
47             //判斷此點是否遍歷過
48             if(points[i].isVisited==false) {
49                 //計算起始點到遍歷點之間的距離,從而更新最小路徑
50                 totalLen += points[i].getLength(p);
51                 
52                 //若小於最小路徑,則更遍歷此點繼續下一步
53                 if(totalLen<minPath) {
54                     //該點的標志位置為true
55                     points[i].isVisited = true;
56                     //每遍歷一個點,計數器加1,起始點更改為遍歷后的點,繼續計算其余點
57                     caculate(points[i], points, totalLen, count+1);
58                 }
59                 //將路徑和倒減,標志置為false,進行下一個方案的計算
60                 totalLen -= points[i].getLength(p);;
61                 points[i].isVisited = false;
62             }
63         }
64         return minPath;
65     }
66     
67     public static void main(String[] args) {
68         int totalLen = 0, count = 0;
69         //從鍵盤獲取輸入
70         Scanner input = new Scanner(System.in);
71         int num = Integer.parseInt(input.nextLine());
72         int n = num;
73         Point[] points = new Point[n];
74         //獲得輸入點的坐標並存入坐標數組中
75         for(int i=0; i<points.length; i++) {
76             String[] strings = input.nextLine().trim().split(",");
77             points[i] = new Point(Integer.parseInt(strings[0]), Integer.parseInt(strings[1]));
78         }
79         
80         //計算最優路徑並打印到控制台
81         minPath = caculate(StartPoint, points, totalLen, count);
82         System.out.println(minPath);
83     }
84 }
85 
86 /*
87  * 以上代碼參考https://blog.csdn.net/s_yj_q/article/details/81132765, 如有錯誤,各路大神多多指教。
88  */
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM