大疆 2023/08/13【笔试真题】

前言

2023-8-23 16:38:33

以下内容源自《【笔试真题】》
仅供学习交流使用

版权

禁止其他平台发布时删除以下此话
本文首次发布于CSDN平台
作者是CSDN@日星月云
博客主页是https://blog.csdn.net/qq_51625007
禁止其他平台发布时删除以上此话

推荐

大疆 2023/08/13

1 无人机环形路线-贪心算法

环形路线,n个充电站,编号0,…,n-1,i号充电站提供charge[i]的电量

无人机从i号充电站,飞往i+1充电站,消耗cost[i]

问无人机是否能够从某一充电站出发,绕一圈返回;
能返回输出起始站,否则返回-1。

输入提供的电量
输入消耗的电量

输入
5
1 2 3 4 5
5
3 4 5 1 2

输出
3

解法

package test0813;

import java.util.*;


/*
输入
5
1 2 3 4 5
5
3 4 5 1 2

输出
3
 */
public class Main1 {
    static class Solution {

        /* Write Code Here */
        public int circle_fly(int[] charge, int[] cost) {
            int sumCharge =0;
            int sumCost =0;
            for(int c:charge) sumCharge +=c;
            for(int c:cost) sumCost +=c;
            if(sumCharge < sumCost) return -1;
            int countStations =charge.length;
            int currentCharge=0;
            int beginStation =0;
            for(int i = 0; i< countStations; i++){
                currentCharge+=charge[i]-cost[i];
                if(currentCharge<0){
                    currentCharge=0;
                    beginStation =i+1;
                }
            }
            return beginStation;

        }
    }

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int res;

        int charge_size = 0;
        charge_size = in.nextInt();
        int[] charge = new int[charge_size];
        for(int charge_i = 0; charge_i < charge_size; charge_i++) {
            charge[charge_i] = in.nextInt();
        }

        if(in.hasNextLine()) {
            in.nextLine();
        }

        int cost_size = 0;
        cost_size = in.nextInt();
        int[] cost = new int[cost_size];
        for(int cost_i = 0; cost_i < cost_size; cost_i++) {
            cost[cost_i] = in.nextInt();
        }

        if(in.hasNextLine()) {
            in.nextLine();
        }

        res = new Solution().circle_fly(charge, cost);
        System.out.println(String.valueOf(res));

    }

}

2 动态规划

LCP 35. 电动车游城市

解答

package test0813;

import java.util.*;

/*



LCP 35.电动车游城市
https://leetcode.cn/problems/DFPeFJ/

小明的电动车电量充满时可行驶距离为 cnt,每行驶 1 单位距离消耗 1 单位电量,且花费 1 单位时间。
小明想选择电动车作为代步工具。地图上共有 N 个景点,景点编号为 0 ~ N-1。
他将地图信息以 [城市 A 编号,城市 B 编号,两城市间距离] 格式整理在在二维数组 paths,表示城市 A、B 间存在双向通路。
初始状态,电动车电量为 0。每个城市都设有充电桩,
charge[i] 表示第 i 个城市每充 1 单位电量需要花费的单位时间。
请返回小明最少需要花费多少单位时间从起点城市 start 抵达终点城市 end。

输入样式:
第一行表示n个path,宽度是3
接下来n行的path
输入cnt
输入start
输入end
输入charge[]的长度
输入charge[]

输出样式
最少单位时间

样例输入
5 3
1 3 3
3 2 1
2 1 3
0 1 4
3 0 5
6
1
0
4
2 10 4 1

样例输入
6 3
0 4 2
4 3 5
3 0 5
0 1 5
3 2 4
1 2 8
8
0
2
5
4 1 1 3 2

样例输出
38
 */


public class Main2 {

    /**
     * 首先建图, 存储每个城市相邻的城市和距离
     *
     * 使用一个二维数组保存结果arr[i][j] = k, i = 所在城市, j = 剩余电量, k = 最短时间
     *
     * 用队列来记录每个路径的信息 {time,cur,power}, time = 已用时间, cur = 所在城市, power = 剩余电量 (使用优先队列来保证每次优先执行已用时间最少的路径)
     *
     * 每次只会有两种操作
     *
     * 充一次电 - 新时间 = 已用时间 + 当前城市每单位充电需要时间, 新电量 = 剩余电量 + 1
     * 去往下一个城市 - 新时间 = 已用时间 + 去往该需要消耗的时间, 新电量 = 剩余电量 − 去往该城市需要消耗的电量
     *
     * 作者:Feilulue 🍒
     * 链接:https://leetcode.cn/problems/DFPeFJ/solutions/974051/java-dijkstra-by-feilue-8p14/
     * 来源:力扣(LeetCode)
     * 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
     */
    static class Solution {
        public int electricCarPlan(int[][] paths, int cnt, int start, int end, int[] charge) {
            int n = charge.length;

            //构造了图
            List<int[]>[] map = new List[n];
            for(int i = 0; i < n; i++) map[i] = new ArrayList();
            for(int[] path : paths){
                map[path[0]].add(new int[]{path[1], path[2]});
                map[path[1]].add(new int[]{path[0], path[2]});
            }

            //使用一个二维数组保存结果arr[i][j] = k
            //i = 所在城市, j = 剩余电量, k = 最短时间
            int[][] res = new int[n][cnt+1];
            for(int[] i : res) Arrays.fill(i, Integer.MAX_VALUE/2);
            res[start][0] = 0;

            //用队列来记录每个路径的信息 {time,cur,power},
            //time = 已用时间, cur = 所在城市, power = 剩余电量
            //(使用优先队列来保证每次优先执行已用时间最少的路径)
            Queue<int[]> queue = new PriorityQueue<int[]>((x, y) -> (x[0] - y[0]));
            queue.offer(new int[]{0, start, 0});

            while(!queue.isEmpty()){
                //取出来
                int[] arr = queue.poll();
                int time = arr[0];
                int cur = arr[1];
                int power = arr[2];

                //继续
                if(time > res[cur][power]) continue;
                //结束
                if(cur == end) return time;

                //充一次电
                //新时间 = 已用时间 + 当前城市每单位充电需要时间, 新电量 = 剩余电量 + 1
                if(power < cnt){
                    int t = time + charge[cur];
                    if(t < res[cur][power+1]){
                        res[cur][power+1] = t;
                        queue.offer(new int[]{t, cur, power+1});
                    }
                }

                //去往下一个城市
                //新时间 = 已用时间 + 去往该需要消耗的时间, 新电量 = 剩余电量 − 去往该城市需要消耗的电量
                for(int[] path : map[cur]){
                    int next = path[0];
                    int cost = path[1];
                    int t = time + cost;
                    int p = power - cost;
                    if(p >= 0 && t < res[next][p]){
                        res[next][p] = t;
                        queue.offer(new int[]{t, next, p});
                    }
                }

            }

            return -1;
        }
    }




    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int res;

        int paths_rows = 0;
        int paths_cols = 0;
        paths_rows = in.nextInt();
        paths_cols = in.nextInt();

        int[][] paths = new int[paths_rows][paths_cols];
        for(int paths_i=0; paths_i<paths_rows; paths_i++) {
            for(int paths_j=0; paths_j<paths_cols; paths_j++) {
                paths[paths_i][paths_j] = in.nextInt();
            }
        }

        if(in.hasNextLine()) {
            in.nextLine();
        }


        int dis;
        dis = Integer.parseInt(in.nextLine().trim());

        int a;
        a = Integer.parseInt(in.nextLine().trim());

        int b;
        b = Integer.parseInt(in.nextLine().trim());

        int charge_size = 0;
        charge_size = in.nextInt();
        int[] charge = new int[charge_size];
        for(int charge_i = 0; charge_i < charge_size; charge_i++) {
            charge[charge_i] = in.nextInt();
        }

        if(in.hasNextLine()) {
            in.nextLine();
        }

        res = new Solution().electricCarPlan(paths, dis, a, b, charge);
        System.out.println(String.valueOf(res));

    }


}

最后

2023-8-23 16:57:51

我们都有光明的未来

祝大家考研上岸
祝大家工作顺利
祝大家得偿所愿
祝大家如愿以偿
点赞收藏关注哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日星月云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值