a星算法c语言实例

时间: 2023-08-31 07:16:42 浏览: 48
以下是一个 A* 算法的 C 语言实现示例: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define ROW 10 #define COL 10 typedef struct { int x; int y; } Point; // 地图,0 表示可以通过,1 表示障碍物 int map[ROW][COL] = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; // 起点和终点 Point start = {0, 0}; Point end = {9, 9}; // 计算两点间的曼哈顿距离 int manhattan_distance(Point a, Point b) { return abs(a.x - b.x) + abs(a.y - b.y); } // 检查点是否在地图内且可以通过 bool is_valid_point(Point p) { if (p.x < 0 || p.x >= ROW || p.y < 0 || p.y >= COL) return false; return map[p.x][p.y] == 0; } // 判断点是否在列表中 bool is_point_in_list(Point p, Point list[], int len) { for (int i = 0; i < len; i++) { if (list[i].x == p.x && list[i].y == p.y) return true; } return false; } // A* 算法 bool a_star() { // 开启列表和关闭列表 Point open_list[ROW * COL]; int open_list_len = 0; Point close_list[ROW * COL]; int close_list_len = 0; // 当前节点和下一个节点 Point current = start; Point next; // 将起点加入开启列表 open_list[open_list_len++] = current; while (open_list_len > 0) { // 从开启列表中找到曼哈顿距离最小的节点 int min_index = 0; for (int i = 1; i < open_list_len; i++) { if (manhattan_distance(open_list[i], end) < manhattan_distance(open_list[min_index], end)) min_index = i; } current = open_list[min_index]; // 如果当前节点是终点,则找到路径 if (current.x == end.x && current.y == end.y) return true; // 将当前节点从开启列表中移除,并加入关闭列表 for (int i = min_index; i < open_list_len - 1; i++) open_list[i] = open_list[i + 1]; open_list_len--; close_list[close_list_len++] = current; // 扩展当前节点的邻居节点 for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { next.x = current.x + i; next.y = current.y + j; // 忽略自身和对角线方向的邻居节点 if ((i == 0 && j == 0) || (i != 0 && j != 0)) continue; // 检查邻居节点是否在地图内且可以通过 if (!is_valid_point(next)) continue; // 检查邻居节点是否在关闭列表中 if (is_point_in_list(next, close_list, close_list_len)) continue; // 计算邻居节点的 f 值 next.f = current.g + 1 + manhattan_distance(next, end); // 检查邻居节点是否已经在开启列表中 int index = -1; for (int k = 0; k < open_list_len; k++) { if (open_list[k].x == next.x && open_list[k].y == next.y) { index = k; break; } } // 如果邻居节点不在开启列表中,则将其加入开启列表 if (index == -1) { next.g = current.g + 1; open_list[open_list_len++] = next; } // 否则,如果邻居节点的 f 值小于开启列表中对应节点的 f 值,则更新开启列表中对应节点的 f 值为邻居节点的 f 值,并更新其父节点为当前节点 else if (next.f < open_list[index].f) { open_list[index].f = next.f; open_list[index].g = current.g + 1; } } } } // 没有找到路径 return false; } int main() { if (a_star()) { printf("找到路径!\n"); } else { printf("没有找到路径!\n"); } return 0; } ``` 在本例中,我们使用一个二维数组来表示地图,0 表示可以通过,1 表示障碍物。起点和终点分别用坐标表示。在 `a_star()` 函数中,我们使用开启列表和关闭列表来实现 A* 算法。开启列表中存储的是待扩展的节点,关闭列表中存储的是已经扩展过的节点。每次从开启列表中找到曼哈顿距离最小的节点进行扩展,直到找到终点或者开启列表为空。在扩展节点时,我们先检查其邻居节点是否在地图内且可以通过,然后判断其是否在关闭列表中,最后计算其 f 值(g 值加上曼哈顿距离),并加入开启列表或者更新开启列表中已有节点的 f 值。

相关推荐

最新推荐

recommend-type

Java编程实现A*算法完整代码

主要介绍了Java编程实现A*算法完整代码,简单介绍了a星算法,然后分享了完整测试代码,具有一定借鉴价值,需要的朋友可以参考下。
recommend-type

Python3 A*寻路算法实现方式

今天小编就为大家分享一篇Python3 A*寻路算法实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

A星算法教程,A*算法介绍

该文档对A*算法进行了详细的介绍。 配有详细的图和文字说明,包教包会。 希望对你有所帮助
recommend-type

初识A星算法--Sunway

写这篇文章的初衷是应一个网友的要求,当然我也发现现在有关人工智能的中文站点实在太少,我在这里抛砖引玉,希望...A*算法在人工智能中是一种典型的启发式搜索算法,为了说清楚A*算法,我看还是先说说何谓启发式算法。
recommend-type

rsa加密解密算法C语言代码

rsa加密解密算法C语言代码 #include #include #include &lt;stdlib.h&gt; #include &lt;time.h&gt; #include #include #define MAX 100 #define LEN sizeof(struct slink) void sub(int a[MAX],int b[MAX] ,int c[MAX] ); ...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。