首页 > 其他分享 >三星研究院机试(Optimal Path)

三星研究院机试(Optimal Path)

时间:2024-07-17 13:57:47浏览次数:8  
标签:10 customers office home Path test Optimal 100 机试

Mr. Kim has to deliver refrigerators to N customers. From the office, he is going to visit all the customers and then return to his home. Each location of the office, his home, and the customers is given in the form of integer coordinates (x,y) (0≤x≤100, 0≤y≤100) . The distance between two arbitrary locations (x1, y1) and (x2, y2) is computed by |x1-x2| + |y1-y2|, where |x| denotes the absolute value of x; for instance, |3|=|-3|=3. The locations of the office, his home, and the customers are all distinct. You should plan an optimal way to visit all the N customers and return to his among all the possibilities.

You are given the locations of the office, Mr. Kim’s home, and the customers; the number of the customers is in the range of 5 to 10. Write a program that, starting at the office, finds a (the) shortest path visiting all the customers and returning to his home. Your program only have to report the distance of a (the) shortest path.

You don’t have to solve this problem efficiently. You could find an answer by looking up all the possible ways. If you can look up all the possibilities well, you will get a perfect score.

[Constraints]

5≤N≤10. Each location (x,y) is in a bounded grid, 0≤x≤100, 0≤y≤100, and x, y are integers.

[Input]

You are given 10 test cases. Each test case consists of two lines; the first line has N, the number of the customers, and the following line enumerates the locations of the office, Mr. Kim’s home, and the customers in sequence. Each location consists of the coordinates (x,y), which is reprensented by ‘x y’.

[Output]

Output the 10 answers in 10 lines. Each line outputs the distance of a (the) shortest path. Each line looks like ‘#x answer’ where x is the index of a test case. ‘#x’ and ‘answer’ are separated by a space.

[I/O Example]

Input (20 lines in total. In the first test case, the locations of the office and the home are (0, 0) and (100, 100) respectively, and the locations of the customers are (70, 40), (30, 10), (10, 5), (90, 70), (50, 20).)


5 ← Starting test case #1

0 0 100 100 70 40 30 10 10 5 90 70 50 20

6 ← Starting test case #2

88 81 85 80 19 22 31 15 27 29 30 10 20 26 5 14

10 ← Starting test case #3

39 9 97 61 35 93 62 64 96 39 36 36 9 59 59 96 61 7 64 43 43 58 1 36

...

#1 200

#2 304

#3 366

...

思路:典型的DFS问题,重点关注客户位置,访问路径全排列,再把最短的距离找出来。

public class Solution{

    /**
     * 5 ← Starting test case #1
     *
     * 0 0 100 100 70 40 30 10 10 5 90 70 50 20
     *
     * @param args
     */
    public static int count;
    public static int ans;
    public static int des;


    public static boolean[] visited; // 访问标记
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int T;
        T = sc.nextInt();
        for (int t = 1; t <= T; t++) {

            count = sc.nextInt();
            int nLocations = count + 2;

            int[][] locations = new int[nLocations][2];

            // 读取地点坐标
            for (int i = 0; i < nLocations; i++) {

                locations[i][0] = sc.nextInt();
                locations[i][1] = sc.nextInt();
            }
            ans = Integer.MAX_VALUE;
            des = 0;
            visited = new boolean[count];

            dfs(locations,0, 0);

            StringBuilder sb = new StringBuilder();
            sb.append("#").append(t).append(" ").append(ans);
            System.out.println(sb);

        }
    }

    public static int destination(int[] i, int[] j) {

        int des = Math.abs(j[1]-i[1])+Math.abs(j[0]-i[0]);

        return des;

    }

    private static void dfs(int[][] locations,int k ,int index) { // k 表示上一个地点的位置

        if (index ==count) {
            ans = Math.min(ans, des);
            return;
        }


        for (int i = 0; i < count; i++) {
            if (visited[i] == true) {
                continue;
            }

            visited[i] = true;
            // 计算距离
            if (index == 0) {
                des += destination(locations[0],locations[i + 2]);
            }else{
                des += destination(locations[k + 2], locations[i + 2]);
            }
            if (index == count - 1) {
                des += destination(locations[i+2],locations[1]);
            }

            dfs(locations, i,index+1);

            visited[i] = false;
            // 还原距离
            if (index == 0) {
                des -= destination(locations[0],locations[i + 2]);
            }else{
                des -= destination(locations[k + 2], locations[i + 2]);
            }
            if (index == count - 1) {
                des -= destination(locations[i+2],locations[1]);
            }

        }

    }

}

标签:10,customers,office,home,Path,test,Optimal,100,机试
From: https://blog.csdn.net/Mr__________xiao/article/details/140384963

相关文章

  • 2024年华为OD机试真题-符号运算-(C++/Java/python)-OD统一考试(C卷D卷)
      2024华为OD机试真题目录-(B卷C卷D卷)-【C++JavaPython】    题目描述给定一个表达式,求其分数计算结果。表达式的限制如下:所有的输入数字皆为正整数(包括0)仅支持四则运算(+-*,/)和括号结果为整数或分数,分数必须化为最简格式(比如6,3/4,7/8,90/7)除数可能为0,如果遇到......
  • python中os.stat().st_size、os.path.getsize()获取文件大小
    一、os.stat().st_sizeos.stat(filePath)返回读取指定文件的相关属性,然后利用stat模块进行处理。importosos.stat('data_feather_ys.feather')#os.stat_result(st_mode=33206,st_ino=3659174697257342,st_dev=2829373452,st_nlink=1,st_uid=0,st_gid=0,st_size=400......
  • 解决找不到RUNPATH下的库的问题
    这个问题困扰了我很久,受GPT4的指点才解决的,这里记录一下解决问题的过程。症状$lddrocksdb-kvexelibfolly.so.0.58.0-dev=>/home/admin/opt/cachelib/lib/libfolly.so.0.58.0-dev(0x00007ff382e00000)libthrift-core.so.1.0.0=>notfound$./rocksdb-......
  • INE - Advanced Penetration Testing learning path
    大智慧没有,小聪明不断。不要解读没有,简化理解也没有,直接复制粘贴,直接抄袭或复用,这叫小聪明。有的人则更加小聪明,跳过理论,直接上手,导致N年以后的职业发展直接葬送掉。创新是难的,你们要把内容翻新一遍,已“原创”的形式交付。就要好好看看他们对于课程开发的后背的整体逻辑。知识点-......
  • springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题
    logback现在基本上已经成为springboot日志框架中使用最多的日志实现,在使用中与各中间件集成的一些注意事项记录如下 一SpringBoot中logback读取application.properties(application.yml)中的属性其中使用的时候发现了一个问题,就是如果使用的lobback配置文件的名称是logb......
  • 题解:AT_abc362_d [ABC362D] Shortest Path 3
    一句话题意:给定一个带点权的有权无向连通图,求点1到所有其它点的最短路径。首先,只有1一个起点,所以是单源最短路,又因为最大是\(2\times10^5\),所以是优先队列(堆)优化过后的Dijkstra。所以,我们只需要解决点权的问题就好了。一种显而易见的想法是把与这条边的边权加上起终点......
  • xpath
    xpathXPath是XML语言中的一种路径,可以用来确定XML文档中部分内容位置的语言XPath注入原理也与SQL注入类似,是网站对未经处理的用户输入进行查询时产生的,用户可提交恶意代码,获取完整的XML文档XPath有很多节点,包括元素节点,属性节点和文本节点语法查询查询loginID为abc......
  • E. Count Paths
    原题链接题解前言:这题可以只调用一遍dfs。首先,以颜色为color_u的u为根结点的子树内,颜色与u颜色相同的结点不能与u的其余子树中颜色为color_u的结点相连接。我们需要一个num数组,num[i]表示当前结点i,有多少个结点可以与他连接。接下来,我们任取一个结点为根结点去跑dfs。假设......
  • E. Count Paths
    原题链接题解对于这种无序点对统计问题,我们可以遍历每一个点,然后计算其与之前遍历过的点的配对\(dfs\)遍历,设\(num[i]\)代表遍历到当前节点时,有多少可与当前节点配对的、节点颜色为\(i\)的、且\(dfs\)序小于当前节点(即之前遍历过的)的节点维护方法:每往子节点遍历,由于子......
  • 2024年华为OD机试真题-传递悄悄话-C++-OD统一考试(C卷D卷)
    2024年OD统一考试(D卷)完整题库:华为OD机试2024年最新题库(Python、JAVA、C++合集) 题目描述:给定一个二叉树,每个节点上站着一个人,节点数字表示父节点到该节点传递悄悄话需要花费的时间。初始时,根节点所在位置的人有一个悄悄话想要传递给其他人,求二叉树所有节点上的人都接收到悄......