追逐法检测单链表中的环

 追逐法检测单链表中的环,不知道还有没有更高效的算法?欢迎拍砖!

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <stdbool.h>
  4
  5 typedef struct linklist{
  6     int data;
  7     struct linklist *next;
  8 }linklist;
  9
 10 bool check_loop(linklist *pHead)
 11 {
 12     if (0 == pHead)
 13         return false;
 14
 15     linklist *pFast = pHead, *pSlow = pHead;
 16
 17     while(0 != pFast)
 18     {
 19         pFast = pFast->next;
 20         pSlow = pSlow->next;
 21         if (0 != pFast){
 22             pFast = pFast->next;
 23             if (pFast == pSlow)
 24                 return true;
 25         }
 26     }
 27     return false;
 28 }
 29
 30 int main()
 31 {
 32     linklist pHead;
 33     linklist *q = 0;
 34     int i;
 35     for(i = 0; i < 10; i++)
 36     {
 37         linklist *p = malloc(sizeof(linklist));
 38         p->data = i;
 39         p->next = 0;
 40         if (9 == i)
 41             p->next = pHead.next;
 42
 43         if (0 == q)
 44             pHead.next = p;
 45         else
 46             q->next = p;
 47
 48         q = p;
 49     }
 50
 51     if(check_loop(&pHead))
 52         printf("There is a loop in the linked lisst./n");
 53     else
 54         printf("No loop/n");
 55
 56     return 0;
 57 }
### 智能车摄像头边线追逐实现原理 智能车通过摄像头捕捉赛道图像并识别车道边界,从而调整行驶方向保持在赛道中央。这一过程依赖于计算机视觉技术来处理实时视频流。 #### 图像采集与预处理 车辆配备前置摄像头用于拍摄前方道路情况。为了提高后续算法效率,在获取原始RGB图像之后通常会将其转换成灰度图以减少数据量[^1]: ```python import cv2 def preprocess_image(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) edged = cv2.Canny(blurred, 50, 150) return edged ``` #### 车道检测 利用边缘检测算子(如Canny)提取图像中的显著变化区域作为潜在的车道线条位置。接着采用霍夫变换直线检测寻找最有可能代表两侧边界的两条平行线段: ```python def detect_lines(edged): lines = cv2.HoughLinesP( edged, rho=1, theta=np.pi / 180, threshold=20, minLineLength=30, maxLineGap=200 ) return lines ``` #### 方向控制逻辑 基于所获得的两侧行车道信息计算出中心点偏移距离以及角度偏差值,据此调整电机转速差使车身逐步回归至理想轨迹上运行。当检测不到有效特征时,则维持当前状态继续前进直至重新找到参考物为止. ```python def control_steering(lines): if lines is not None and len(lines) >= 2: line_left = lines[0][0] line_right = lines[-1][0] center_x = int((line_left[0]+line_right[0])/2) error = image_center - center_x steering_angle = pid_controller.update(error) set_motor_speed(steering_angle) else: keep_straight() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值