【发布时间】:2020-03-12 14:35:17
【问题描述】:
我正在做一个需要解决迷宫的项目,并将解决方案打印在标准输出上:
为此,我使用了 A* (astar) 算法,没有成本问题,因为我不关心找到最短路径,因为迷宫已经解决。所以我只有两个链表,其中包含节点,它们是地图上的位置。
无论如何,它现在正在工作,但我想提高我在更大迷宫上的时间,因为在 500x500 迷宫上需要超过 10 秒,而在 1000x1000 迷宫上需要超过一分钟,我已经做了一些事情,例如优化我的 add_node 函数,或者调用 in_list 函数来搜索节点是否已经在关闭列表中,但是,即使它的速度翻了一番或三倍,这显然还不够 A* 算法应该更快。
这是我用 C 语言实现的程序:
//my header's structures:
typedef struct node_s
{
int x;
int y;
} node_t;
typedef struct maze_s
{
char **map;
int dim[2];
node_t start;
node_t objective;
node_t *current;
bool states[4];
} maze_t;
typedef struct linked_list_s
{
node_t node;
node_t *parent;
struct linked_list_s *next;
} linked_list_t;```
//And here The Most Important Utils Functions:
void in_closed(linked_list_t *closed_list, maze_t *maze)
{
linked_list_t *tmp = closed_list;
node_t *crt = maze->current;
if (!tmp)
return;
while (tmp) {
if (tmp->node.x == crt->x + 1 && tmp->node.y == crt->y) {
maze->states[0] = true;
}
if (tmp->node.x == crt->x && tmp->node.y == crt->y + 1) {
maze->states[1] = true;
}
if (tmp->node.x == crt->x && tmp->node.y == crt->y - 1) {
maze->states[2] = true;
}
if (tmp->node.x == crt->x - 1 && tmp->node.y == crt->y) {
maze->states[3] = true;
}
tmp = tmp->next;
}
}
linked_list_t *add_node(linked_list_t *list, node_t node, node_t *parent)
{
linked_list_t *new = malloc(sizeof(linked_list_t));
new->node = node;
new->parent = parent;
new->next = list;
return (new);
}
linked_list_t *remove_node(linked_list_t *list, node_t *node)
{
linked_list_t *head = list;
if (list->node.x == node->x && list->node.y == node->y) {
list = list->next;
return (list);
}
while (list->next) {
if (list->next->node.x == node->x && list->next->node.y == node->y) {
list->next = list->next->next;
return (head);
} else {
list = list->next;
}
}
return (NULL);
}
//And then the main algorithm (all the functions that I didn't show were not important for my problem) :
linked_list_t *check_neighbour(int neighbour, maze_t *maze,
linked_list_t *list, linked_list_t *closed_list)
{
node_t *crt = maze->current;
if (neighbour == 1 && crt->x + 1 < maze->dim[0] && !maze->states[0]
&& maze->map[crt->x + 1][crt->y] == '*') {
list = add_node(list, (node_t){crt->x + 1, crt->y}, crt);
}
if (neighbour == 2 && crt->y + 1 < maze->dim[1] && !maze->states[1]
&& maze->map[crt->x][crt->y + 1] == '*') {
list = add_node(list, (node_t){crt->x, crt->y + 1}, crt);
}
if (neighbour == 0 && crt->y > 0 && !maze->states[2]
&& maze->map[crt->x][crt->y - 1] == '*') {
list = add_node(list, (node_t){crt->x, crt->y - 1}, crt);
}
if (neighbour == 3 && crt->x > 0 && !maze->states[3]
&& maze->map[crt->x - 1][crt->y] == '*') {
list = add_node(list, (node_t){crt->x - 1, crt->y}, crt);
}
return (list);
}
void end(maze_t *maze, linked_list_t *closed_list)
{
linked_list_t *tmp = closed_list;
bool cond = true;
for (int x = maze->objective.x, y = maze->objective.y; tmp->next;) {
if (tmp->node.x == x && tmp->node.y == y) {
maze->map[x][y] = 'o';
x = tmp->parent->x;
y = tmp->parent->y;
closed_list = remove_node(closed_list, &tmp->node);
tmp = closed_list;
cond = false;
}
if (cond) {
tmp = tmp->next;
} else {
cond = true;
}
}
maze->map[0][0] = 'o';
}
linked_list_t *solve_maze(maze_t *maze, linked_list_t *list,
linked_list_t *closed_list)
{
while (list) {
if (list->node.x == maze->objective.x
&& list->node.y == maze->objective.y)
return (closed_list);
maze->current = &list->node;
closed_list = add_node(closed_list, list->node, list->parent);
in_closed(closed_list, maze);
for (int i = 0; i < 4; i++)
list = check_neighbour(i, maze, list, closed_list);
for (int i = 0; i < 4; i++)
maze->states[i] = false;
list = remove_node(list, maze->current);
}
return(NULL);
}
int solver(char **av)
{
int *dim = get_dimensions(av[1]);
maze_t maze = {.dim = {dim[0], dim[1]}, .map = get_map(av[1], dim),
.start = {0, 0}, .objective = {dim[0] - 1, dim[1] - 1}, .states =
{false, false, false, false}};
linked_list_t *list = get_open_list(&maze);
linked_list_t *closed_list = NULL;
if (maze.map[0][0] == 'X' || maze.map[dim[0] - 1][dim[1] - 1] == 'X')
return (printf("no solution found\n"));
closed_list = solve_maze(&maze, list, closed_list);
if (!closed_list)
return (printf("no solution found\n"));
closed_list = add_node(closed_list, maze.objective, &closed_list->node);
printf("algorithm done\n");
end(&maze, closed_list);
printf("end finished\n");
print_map_color(&maze);
return (0);
}
【问题讨论】:
-
如果你删除了 A* 的成本部分,那么你就没有 A* 了,你就有了深度优先搜索。
-
我改了标题
-
您的代码的主要问题是您使用了访问集的列表。您应该使用一个布尔数组,每个顶点都有一个槽,未访问为 false,已访问为 true。此外,代码比必要的复杂得多
标签: c optimization breadth-first-search maze