请用c语言实现并写出以下代码:实现Dijkstra算法、A*算法
时间: 2024-04-22 12:25:02 浏览: 88
当然可以!下面是使用 C 语言实现 Dijkstra 算法和 A* 算法的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define V 9
// Dijkstra算法实现
int minDistance(int dist[], int sptSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == 0 && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
void printSolution(int dist[]) {
printf("Vertex \t\t Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}
void dijkstra(int graph[V][V], int src) {
int dist[V];
int sptSet[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = 0;
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = 1;
for (int v = 0; v < V; v++) {
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
}
printSolution(dist);
}
// A*算法实现
typedef struct Node {
int x, y; // 坐标
int f, g, h; // 评估值 f = g + h
struct Node *parent; // 父节点
} Node;
int manhattanDistance(int x1, int y1, int x2, int y2) {
return abs(x1 - x2) + abs(y1 - y2);
}
void aStar(int maze[V][V], Node start, Node end) {
int openList[V][V];
int closedList[V][V];
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
openList[i][j] = 0;
closedList[i][j] = 0;
}
}
openList[start.x][start.y] = 1;
Node *current;
while (openList[end.x][end.y] != 1) {
int minF = INT_MAX;
int x, y;
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (openList[i][j] == 1) {
int f = maze[i][j] + manhattanDistance(i, j, end.x, end.y);
if (f <= minF) {
minF = f;
x = i;
y = j;
}
}
}
}
openList[x][y] = 0;
closedList[x][y] = 1;
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0)
continue;
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < V && ny >= 0 && ny < V && maze[nx][ny] != -1 && closedList[nx][ny] != 1) {
int gNew = maze[nx][ny];
int hNew = manhattanDistance(nx, ny, end.x, end.y);
int fNew = gNew + hNew;
if (openList[nx][ny] == 0 || fNew < maze[nx][ny]) {
Node *child = (Node*)malloc(sizeof(Node));
child->x = nx;
child->y = ny;
child->f = fNew;
child->g = gNew;
child->h = hNew;
child->parent = current;
openList[nx][ny] = 1;
}
}
}
}
}
// 输出路径
printf("Path: ");
current = current->parent;
while (current != NULL) {
printf("(%d,%d) ", current->x, current->y);
current = current->parent;
}
}
int main() {
// Dijkstra算法
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
dijkstra(graph, 0);
// A*算法
int maze[V][V] = { {1,1,-1,-1,-1,-1,-1,-1,-1},
{1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,-1,-1,-1,-1,-1,-1,-1,-1},
{2,2,2,2,2,2,2,2,2} };
Node start = {0, 0, 0, 0, 0, NULL};
Node end = {8, 8, 0, 0, 0, NULL};
aStar(maze, start, end);
return 0;
}
```
这段代码实现了 Dijkstra 算法和 A* 算法。你可以根据自己的需求进行修改和调整。希望对你有帮助!
阅读全文