规划控制算法工程师 代码面 面经

技术面的面经到处都有了,我就不提了。这边总结一些力扣上没有的代码面的题目

题目1:假设一辆车沿一条直路向前行驶,初始位置为0m,速度为0m/s,每一秒该车都可以选择一次加速度:+2m/s^2,-2m/s^2,或者0m/s^2。该车在1秒内开过的路程是(v(t)+v(t+1))/2m。终点在1000m处, 且到达终点时速度恰好为0m/s。路上有若干处限速,请问如何加减速才能保证最快到达终点且到达终点时速度为0m/s。

我的思路:

编一个类似仿真的环境,一步=1秒。

每一步都依次考察+1,0,-1 三种加速度是否满足限速要求。一旦找到满足要求的加速度,就不用再考察更慢的决策了,直接执行既可。

这里的考察包含两点

  1. 考察当前仿真步内的决策是否覆盖限速点且不超速,
  2. 考察下一步的起始车速是否能保证在未来连续减速的策略下不会在前方的任意限速点超速。 那如何保证这第二点呢,其实就是依次检查所有限速点,假设下一步开始连续以-1减速,是否会在该限速点超速。 

终点时速度恰好为0这个要求起始也可以看作是一种限速 。

下面是我用C++实现的代码 (我的代码有点小问题,我就假设终点速度要低于2)。

#include <iostream>
#include <cstdio>
#include <unordered_map>
#include <math.h>
#include <array>


int main()
{
    std::unordered_map<int, int> v_limit_vec{ {100,5}, {405,20} ,{1000,2} }; //<speed limit position, speed limit value>
    int pos = 0; //initial position
    int t = 0;//time
    int v = 0;//initial speed
    int v_next = 0; //next step speed
    int p_next = 0; //next step position
    std::array<int,3> a_vec{ 1,0,-1 };// three actions
    while (pos<1000){
        for (int a : a_vec){ //start with +1, then 0, then -1
            v_next = v + a;
            if (v_next < 0) {
                v_next = 0;
            }
            p_next = (v_next + v) / 2 + pos;

            bool action_good = true; //once find good action, break action loop
            for (std::pair<int,int> the_pair : v_limit_vec) {//loop over speed limits
                int v_lim_pos=the_pair.first; //get speed limit position
                if (v_lim_pos - pos <= 0)  {
                    continue;
                }
                int v_lim_v=the_pair.second; //get speed limit speed
                if (v_lim_pos - p_next > 0) { //if potential speeding at future step

                    //compute the acc of constant deceleration to satisfy speed limit
                    float acc = (static_cast<float>(v_lim_v) * v_lim_v - v_next * v_next) / (2 * (v_lim_pos - p_next));
                    if (acc < -1) {
                        action_good = false;
                    }
                } 
                else { //if speeding at current step
                    if (v_next > v_lim_v) {
                        action_good = false;
                    }
                }
                if (!action_good) { 
                    break; //this action does not satisfy this speed limit, no need to check other speed limits.
                }
            }
            if (action_good) {
                break;
            }
        }
        //simulate one step
        t = t + 1;
        v = v_next;
        pos = p_next;
    }
    std::cout << t <<std::endl;
    std::cout << v << std::endl;
    std::cout << pos << std::endl;
}

我编程水平挺菜的,大家凑活看。

这个问题可能用动态规划解会更好,欢迎各位大佬抛砖引玉。

题目2: 给定一系列[-pi, pi] 间的角度,求角度的平均值。  

解题思路:这题很tricky,不能直接求和以后求平均,因为角度的平均和一般平均不一样。 我查阅了一些资料,说要用向量法做, 就是把所有角度写成该角度的单位向量,(cos(theta), sin(theta)), 然后加起来,加完之后的朝向就是角度的平均朝向。

中文资料不多,具体大家可以看这篇英文资料Averaging Angles – The Math Doctors。也欢迎留言讨论。

还有几道题,等我有空也总结一下

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值