请用C++实现广度优先算法、深度优先算法和A*算法
时间: 2023-05-26 20:07:43 浏览: 189
A*算法的C++实现
由于题目不确定细节,以下是我自己实现的简单版本,供参考:
1. 广度优先算法(BFS)
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_N 100
#define INF 1e9
int n, m; /* n: 点数,m: 边数 */
int graph[MAX_N][MAX_N]; /* 邻接矩阵 */
int dist[MAX_N]; /* 距离数组 */
int visited[MAX_N]; /* 是否访问过 */
int queue[MAX_N]; /* 队列 */
int front = 0, rear = -1;
void bfs(int start) {
for (int i = 0; i < n; ++i) {
dist[i] = INF; /* 初始化距离数组 */
visited[i] = 0;
}
dist[start] = 0; /* 起点距离为0 */
visited[start] = 1;
queue[++rear] = start; /* 入队 */
while (front <= rear) { /* 队列非空 */
int cur = queue[front++]; /* 出队 */
for (int i = 0; i < n; ++i) {
if (graph[cur][i] && !visited[i]) { /* 如果存在边且未访问过 */
dist[i] = dist[cur] + 1; /* 更新距离 */
visited[i] = 1;
queue[++rear] = i; /* 入队 */
}
}
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d %d", &u, &v);
graph[u][v] = 1; /* 无向图,建双向边 */
graph[v][u] = 1;
}
bfs(0);
for (int i = 0; i < n; ++i) {
printf("%d ", dist[i]);
}
printf("\n");
return 0;
}
```
2. 深度优先算法(DFS)
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_N 100
int n, m;
int graph[MAX_N][MAX_N]; /* 邻接矩阵 */
int visited[MAX_N]; /* 是否访问过 */
void dfs(int cur) {
printf("%d ", cur); /* 访问当前节点 */
visited[cur] = 1;
for (int i = 0; i < n; ++i) {
if (graph[cur][i] && !visited[i]) { /* 如果存在边且未访问过 */
dfs(i); /* 递归深搜 */
}
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d %d", &u, &v);
graph[u][v] = 1; /* 无向图,建双向边 */
graph[v][u] = 1;
}
dfs(0);
return 0;
}
```
3. A*算法
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_N 100
#define INF 1e9
typedef struct {
int x;
int y;
} Point;
int n, m;
Point points[MAX_N]; /* 点数组 */
int graph[MAX_N][MAX_N]; /* 邻接矩阵 */
int dist[MAX_N]; /* 距离数组 */
int visited[MAX_N]; /* 是否访问过 */
/* 计算两个点之间的欧几里得距离 */
double dist_point(Point p1, Point p2) {
double dx = (double) (p1.x - p2.x);
double dy = (double) (p1.y - p2.y);
return sqrt(dx * dx + dy * dy);
}
/* 启发函数,估计点x到点y的距离 */
double heuristic(int x, int y) {
return dist_point(points[x], points[y]);
}
/* 优先队列实现(小根堆) */
typedef struct {
int n; /* 当前节点数量 */
int a[MAX_N]; /* 节点数组 */
double f[MAX_N]; /* 估价函数值 */
} Heap;
void heap_init(Heap *h) {
h->n = 0;
}
void heap_push(Heap *h, int x, double f) {
int i = ++h->n;
while (i > 1 && f < h->f[i / 2]) { /* 上浮操作 */
h->a[i] = h->a[i / 2];
h->f[i] = h->f[i / 2];
i /= 2;
}
h->a[i] = x;
h->f[i] = f;
}
int heap_pop(Heap *h) {
int min = h->a[1];
int x = h->a[h->n--];
int i = 1, j = 2;
while (j <= h->n) { /* 下沉操作 */
if (j < h->n && h->f[j] > h->f[j + 1]) {
j++;
}
if (h->f[j] >= h->f[i]) {
break;
}
h->a[i] = h->a[j];
h->f[i] = h->f[j];
i = j;
j *= 2;
}
h->a[i] = x;
return min;
}
int heap_empty(Heap *h) {
return h->n == 0;
}
void a_star(int start, int end) {
for (int i = 0; i < n; ++i) {
dist[i] = INF; /* 初始化距离数组 */
visited[i] = 0;
}
Heap heap;
heap_init(&heap);
dist[start] = 0; /* 起点距离为0 */
heap_push(&heap, start, heuristic(start, end)); /* 将起点加入队列 */
while (!heap_empty(&heap)) { /* 队列非空 */
int cur = heap_pop(&heap); /* 取出优先级最高的节点 */
visited[cur] = 1;
if (cur == end) { /* 到达终点 */
return;
}
for (int i = 0; i < n; ++i) {
if (graph[cur][i] && !visited[i]) { /* 如果存在边且未访问过 */
double new_dist = dist[cur] + dist_point(points[cur], points[i]);
if (new_dist < dist[i]) {
dist[i] = new_dist;
heap_push(&heap, i, dist[i] + heuristic(i, end)); /* 加入队列 */
}
}
}
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; ++i) {
int x, y;
scanf("%d %d", &x, &y);
points[i].x = x;
points[i].y = y;
}
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d %d", &u, &v);
graph[u][v] = 1; /* 无向图,建双向边 */
graph[v][u] = 1;
}
a_star(0, n - 1);
printf("%d\n", dist[n - 1]);
return 0;
}
```
阅读全文