算法笔记:A*算法

A*算法是一种很常用的路径查找和图形遍历算法。它有较好的性能和准确度

1 中心思路

  • A*算法通过下面这个函数来计算每个节点n的优先级
    • f(n)=g(n)+h(n)
      • f(n)是节点n的综合优先级。当选择下一个要遍历的节点时,总会选取综合优先级最高(f(n)值最小)的节点。
      • g(n) 是节点n距离起点的代价
      • h(n)是节点n距离终点的预计代价,这也就是A*算法的启发函数
  • A*算法在运算过程中,每次从优先队列中选取f(n)值最小(优先级最高)的节点作为下一个待遍历的节点。

1.1  伪代码

  • A*算法使用两个集合来表示待遍历的节点(open_set),与已经遍历过的节点(close_set)

  • 初始化open_set和close_set
  • 将起点n0加入open_set中,并设置f(n0)=0(优先级最高)
  • 如果open_set不为空,则从open_set中选取优先级最高的节点n:
    • 如果节点n为终点:
      • 从终点开始逐步追踪parent节点,一直达到起点
      • 返回找到的结果路径,算法结束
    • 如果节点n不是终点:
      • 将节点n从open_set中删除,并加入close_set中
      • 遍历节点n所有的邻近节点
        • 如果邻近节点m在close_set中(已经访问过来),则:
          • 跳过,选取下一个邻近节点
        • 如果邻近节点m也不在open_set中,则=
          • 设置节点m的parent为节点n
          • 计算节点m的优先级f(n)
          • 将节点m加入open_set中

2 启发函数

  • 在极端情况下,当启发函数h(n)始终为0,则将由g(n)决定节点的优先级,此时算法就退化成了Dijkstra算法
  • 如果h(n)始终小于等于节点n到终点的代价,则A*算法保证一定能够找到最短路径。
    • 但是当h(n)的值越小,算法将遍历越多的节点,也就导致算法越慢。
  • 如果h(n)完全等于节点n到终点的代价,则A*算法将找到最佳路径,并且速度很快。
    • 可惜的是,并非所有场景下都能做到这一点。因为在没有达到终点之前,我们很难确切算出距离终点还有多远。
  • 如果h(n)的值比节点n到终点的代价要大,则A*算法不能保证找到最短路径,不过此时会很快
    • 比如这种情况最短路路径应该是下路
    • 但如果我们估计的h(n)为实际路径的两倍
      • 那么选择中间节点的时候,A*算法会选择上路 (5+3*2=11,4+3.6*2=11.2)

     

2.1 一些经验启发函数

  • 如果图形中只允许朝上下左右四个方向移动,则可以使用曼哈顿距离
  • 如果图形中允许朝八个方向移动,则可以使用对角距离
  • 如果图形中允许朝任何方向移动,则可以使用欧几里得距离

     

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
此为用C#写的A*算法源代码 using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace EtSoft.AStarLib { public class AStar { private StarNodeCollection openList = new StarNodeCollection(); private StarNodeCollection closeList = new StarNodeCollection(); public StarNode currentNode = null; #region 构造函数 /// <summary> /// 使用指定的地图对象、起点和终点初始化A星算法 /// </summary> /// <param name="map">地图对象</param> public AStar(Map map) { this.map = map; } /// <summary> /// /// </summary> /// <param name="map">地图对象</param> /// <param name="start">起点坐标</param> /// <param name="end">终点坐标</param> public AStar(Map map, Point start, Point end) : this(map) { this.start = new StarNode(start); this.end = new StarNode(end); openList.Add(new StarNode(start)); //AddStartNodeToOpenList(this.start); } /// <summary> /// /// </summary> /// <param name="map">地图对象</param> /// <param name="startX">起点X坐标</param> /// <param name="startY">起点Y坐标</param> /// <param name="endX">终点X坐标</param> /// <param name="endY">终点Y坐标</param> public AStar(Map map, int startX, int startY, int endX, int endY) : this(map, new Point(startX, startY), new Point(endX, endY)) { } #endregion #region 属性 protected Map map; /// <summary> /// 地图数据 /// </summary> public Map Map { get { return map; } set { map = value; } } private StarNode start = null; /// <summary> /// 起点坐标,如果没有设置起点,返回null /// </summary> public StarNode Start { get { return start; } set { start = value; openList.Clear(); openList.Add(start); //AddNodeToOpenList(start); } } private StarNode end = null; /// <summary> /// 终点坐标,如果没有设置终点,返回null /// </summary> public StarNode End { get { return end; } set { end = value; } } private StarNodeCollection path; /// <summary> /// 返回路径节点集合,没有路径则返回null /// </summary> public StarNodeCollection Path { get { return path; } } private bool allDirection = false; /// <summary> /// true,允许八方向寻路 /// </summary> public bool AllDirection { get { return allDirection; } set { allDirection = value; } } #endregion /// <summary> /// 开始寻路 /// </summary> public void StartSearchPath() { if (start == null) throw new InvalidNodeException(InvalidNodeTypes.NoStart); if (end == null) throw new InvalidNodeException(InvalidNodeTypes.NoEnd); path = null; openList.Clear(); closeList.Clear(); currentNode = start; SearchPath(currentNode); } //寻路递归,受保护的虚方法,允许在子类重写寻路算法 protected virtual void SearchPath(StarNode starNode) { //currentNode = starNode; openList.Remove(starNode); closeList.Add(starNode); AddNodeToOpenList(); if (closeList.Contains(end)) { //如果终点在关闭列表中,找到路径 StarNode node=starNode.Parent; path = new StarNodeCollection(); do { path.Add(node); node = node.Parent; } while (node != null && !node.Equals(start)); path.Reverse(); return; } else if (openList.Count == 0) { //终点不在关闭列表,开放列表已空,没有可通行的路径 return; } currentNode = GetNextNode(); //迭代过程 SearchPath(currentNode); } /// <summary> /// 获得当前节点的下一个最佳节点 /// </summary> /// <returns></returns> public StarNode GetNextNode() { openList.SortByF(); return openList[0]; } /// <summary> /// 把当前点周围可通过的点加入到开放列表中 /// </summary>
题目描述: 给定一篇手写笔记的图片,要求用代码实现将其还原成文字。 示例: 输入: ![handwriting](https://cdn.luogu.com.cn/upload/image_hosting/jz2h2y1f.png) 输出: ``` Hello, world! This is a handwriting recognition problem. Can you solve it? Let's see. 1. First, let's import some libraries. - numpy: a popular library for numerical computing - PIL: Python Imaging Library, for image processing - tensorflow: a popular library for machine learning 2. Next, let's load the image and preprocess it. - Convert it to grayscale - Resize it to a fixed size - Invert the colors 3. Now we can use a pre-trained OCR model to recognize the text. - We'll use Tesseract, an open-source OCR engine. - It works best on clean, high-quality images, so we'll need to do some post-processing. 4. Finally, let's print the recognized text and see how well we did. - We should be able to get pretty good accuracy if the image is clear and the text is legible. ``` 解题思路: 1. 首先需要加载一些常用的 Python 库,如 numpy、PIL 和 tensorflow。 2. 接着需要读取手写笔记的图片,并对其进行预处理。具体来说,需要将其转换为灰度图像、调整大小并反转颜色。 3. 接下来,可以使用一个预训练的 OCR 模型来识别文本。这里使用的是 Tesseract,一个开源的 OCR 引擎。需要注意的是,Tesseract 在处理高质量、清晰的图像时表现最佳,因此在处理后需要进行一些后处理。 4. 最后,将识别出的文本打印出来,以便进行检查。 代码实现: ```python import numpy as np from PIL import Image import pytesseract # Step 1: Load the image and preprocess it img = Image.open('handwriting.png').convert('L') img = img.resize((int(img.size[0] * 2), int(img.size[1] * 2))) img = ImageOps.invert(img) # Step 2: Use Tesseract to recognize the text text = pytesseract.image_to_string(img) # Step 3: Post-process the recognized text text = text.replace('\n\n', '\n') text = text.strip() # Step 4: Print the recognized text print(text) ``` 其中,`pytesseract` 库需要提前安装。在 Ubuntu 系统下,可以使用以下命令进行安装: ``` sudo apt install tesseract-ocr sudo apt install libtesseract-dev pip install pytesseract ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

UQI-LIUWJ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值