写一个迷宫游戏自动寻路的c语言函数
时间: 2024-02-15 13:02:59 浏览: 52
好的,下面是一个简单的迷宫游戏自动寻路的C语言函数的实现。
假设我们有一个迷宫地图,其中0表示可以通过的空格,1表示障碍物不能通过。我们要实现一个函数,输入起点和终点坐标,输出一条从起点到终点的最短路径。
首先,我们需要定义一个结构体表示坐标:
```
typedef struct {
int x;
int y;
} Point;
```
然后,我们需要定义一个队列来存储待访问的节点:
```
typedef struct {
Point *data;
int front;
int rear;
} Queue;
```
接着,我们需要实现一个BFS算法来搜索最短路径:
```
int BFS(int **maze, int m, int n, Point start, Point end, int **path) {
int **visited = (int **)malloc(sizeof(int *) * m);
for (int i = 0; i < m; i++) {
visited[i] = (int *)malloc(sizeof(int) * n);
memset(visited[i], 0, sizeof(int) * n);
}
Queue *queue = createQueue(m * n);
enqueue(queue, &start);
visited[start.x][start.y] = 1;
while (!isEmpty(queue)) {
Point *current = dequeue(queue);
if (current->x == end.x && current->y == end.y) {
free(queue->data);
free(queue);
for (int i = 0; i < m; i++) {
free(visited[i]);
}
free(visited);
return 1;
}
Point *next;
// 上
if (current->x > 0 && maze[current->x - 1][current->y] == 0 && !visited[current->x - 1][current->y]) {
next = (Point *)malloc(sizeof(Point));
next->x = current->x - 1;
next->y = current->y;
enqueue(queue, next);
visited[next->x][next->y] = 1;
path[next->x][next->y] = current->x * n + current->y + 1;
}
// 下
if (current->x < m - 1 && maze[current->x + 1][current->y] == 0 && !visited[current->x + 1][current->y]) {
next = (Point *)malloc(sizeof(Point));
next->x = current->x + 1;
next->y = current->y;
enqueue(queue, next);
visited[next->x][next->y] = 1;
path[next->x][next->y] = current->x * n + current->y + 1;
}
// 左
if (current->y > 0 && maze[current->x][current->y - 1] == 0 && !visited[current->x][current->y - 1]) {
next = (Point *)malloc(sizeof(Point));
next->x = current->x;
next->y = current->y - 1;
enqueue(queue, next);
visited[next->x][next->y] = 1;
path[next->x][next->y] = current->x * n + current->y + 1;
}
// 右
if (current->y < n - 1 && maze[current->x][current->y + 1] == 0 && !visited[current->x][current->y + 1]) {
next = (Point *)malloc(sizeof(Point));
next->x = current->x;
next->y = current->y + 1;
enqueue(queue, next);
visited[next->x][next->y] = 1;
path[next->x][next->y] = current->x * n + current->y + 1;
}
free(current);
}
free(queue->data);
free(queue);
for (int i = 0; i < m; i++) {
free(visited[i]);
}
free(visited);
return 0;
}
```
最后,我们可以定义一个主函数来测试我们的函数:
```
int main() {
int m = 5;
int n = 5;
int **maze = (int **)malloc(sizeof(int *) * m);
for (int i = 0; i < m; i++) {
maze[i] = (int *)malloc(sizeof(int) * n);
}
maze[0][0] = 0;
maze[0][1] = 0;
maze[0][2] = 0;
maze[0][3] = 0;
maze[0][4] = 0;
maze[1][0] = 0;
maze[1][1] = 1;
maze[1][2] = 1;
maze[1][3] = 1;
maze[1][4] = 0;
maze[2][0] = 0;
maze[2][1] = 0;
maze[2][2] = 0;
maze[2][3] = 0;
maze[2][4] = 0;
maze[3][0] = 0;
maze[3][1] = 1;
maze[3][2] = 1;
maze[3][3] = 1;
maze[3][4] = 0;
maze[4][0] = 0;
maze[4][1] = 0;
maze[4][2] = 0;
maze[4][3] = 0;
maze[4][4] = 0;
int **path = (int **)malloc(sizeof(int *) * m);
for (int i = 0; i < m; i++) {
path[i] = (int *)malloc(sizeof(int) * n);
memset(path[i], 0, sizeof(int) * n);
}
Point start = {0, 0};
Point end = {4, 4};
if (BFS(maze, m, n, start, end, path)) {
Point current = end;
while (current.x != start.x || current.y != start.y) {
printf("(%d, %d)\n", current.x, current.y);
int prev = path[current.x][current.y] - 1;
current.x = prev / n;
current.y = prev % n;
}
printf("(%d, %d)\n", current.x, current.y);
} else {
printf("No path found!\n");
}
for (int i = 0; i < m; i++) {
free(maze[i]);
free(path[i]);
}
free(maze);
free(path);
return 0;
}
```
注意:这只是一个简单的实现,可能并不是最优解,也可能存在一些问题,需要根据具体情况进行修改和优化。
阅读全文