C++ C gets循环中的广度优先搜索
我试图在C语言中实现广度优先搜索算法,搜索从二进制矩阵中第一列白色255点到最后一列的路径 我已经创建了一个算法,可以根据直方图正确地对图像进行二值化,但是现在当我试图找到最短路径时,我遇到了一些问题。也许我不太明白该怎么做 我有一个网格,它是一个包含二进制值0或255的矩阵,其中255表示我可以行走,0表示它是一堵墙。我有地图,知道我能去哪里,我已经去了哪里。两个都有相同的尺寸 我将粘贴的BFS算法的代码,并插入所有的代码,如果你想查看,或为其他人提供进一步的参考 Obs:Coordenada的意思是坐标 链接要点: 感谢阅读:C++ C gets循环中的广度优先搜索,c++,c,algorithm,dijkstra,breadth-first-search,C++,C,Algorithm,Dijkstra,Breadth First Search,我试图在C语言中实现广度优先搜索算法,搜索从二进制矩阵中第一列白色255点到最后一列的路径 我已经创建了一个算法,可以根据直方图正确地对图像进行二值化,但是现在当我试图找到最短路径时,我遇到了一些问题。也许我不太明白该怎么做 我有一个网格,它是一个包含二进制值0或255的矩阵,其中255表示我可以行走,0表示它是一堵墙。我有地图,知道我能去哪里,我已经去了哪里。两个都有相同的尺寸 我将粘贴的BFS算法的代码,并插入所有的代码,如果你想查看,或为其他人提供进一步的参考 Obs:Coordenada
为了获得更多的参考,寻找最短路径是一个众所周知的问题,有很好的解决方案。搜索Dijkstra或者这个A*Loki的特化,我不知道如何在矩阵中实现Dijsktra。该算法使用图形,矩阵只是一种表示。它描述了一个图形。你有一段代码bfs,它实际上在寻找下一个节点,并像dijkstraLoki一样推送队列,如果你能给我看一些伪代码,这是可能的吗?很抱歉,但是我想我没有正确理解如何解决这个问题。这里有一些psudo代码,这些代码在node.arcs中foreacharc,您调用bfs来生成下一个节点。
Coordenada bfs( unsigned char ** grid, Coordenada local, Queue queue, int8_t **map, unsigned long height, unsigned long width)
{
/* Insere a coordenada atual na queue */
queue.push(&queue, local);
/* While queue are not empty */
while ( queue.size != 0 )
{
/* Retrieve some point */
Coordenada p = queue.pop(&queue);
printf("Processando (%d, %d)\n", p.x, p.y);
/* Check if it's the last point */
if ( p.x == 33 - 1 && p.y == 16 - 1 )
{
printf("%s\n", "Chegamos onde queríamos");
return p;
}
/* Try to move on */
if ( isFree(grid, map, p.y + 1, p.x, height, width) )
{
map[p.y][p.x] = -1;
Coordenada next_point;
next_point.y = p.y + 1;
next_point.x = p.x;
queue.push(&queue, next_point);
}
if ( isFree(grid, map, p.y - 1, p.x, height, width) )
{
map[p.y][p.x] = -1;
Coordenada next_point;
next_point.y = p.y - 1;
next_point.x = p.x;
queue.push(&queue, next_point);
}
if ( isFree(grid, map, p.y + 1, p.x + 1, height, width) )
{
map[p.y][p.x] = -1;
Coordenada next_point;
next_point.y = p.y + 1;
next_point.x = p.x + 1;
queue.push(&queue, next_point);
}
if ( isFree(grid, map, p.y - 1, p.x + 1, height, width) )
{
map[p.y][p.x] = -1;
Coordenada next_point;
next_point.y = p.y - 1;
next_point.x = p.x + 1;
queue.push(&queue, next_point);
}
if ( isFree(grid, map, p.y + 1, p.x - 1, height, width) )
{
map[p.y][p.x] = -1;
Coordenada next_point;
next_point.y = p.y + 1;
next_point.x = p.x - 1;
queue.push(&queue, next_point);
}
if ( isFree(grid, map, p.y - 1, p.x - 1, height, width) )
{
map[p.y][p.x] = -1;
Coordenada next_point;
next_point.y = p.y - 1;
next_point.x = p.x - 1;
queue.push(&queue, next_point);
}
if ( isFree(grid, map, p.y, p.x - 1, height, width) )
{
map[p.y][p.x] = -1;
Coordenada next_point;
next_point.y = p.y;
next_point.x = p.x - 1;
queue.push(&queue, next_point);
}
if ( isFree(grid, map, p.y, p.x + 1, height, width) )
{
map[p.y][p.x] = -1;
Coordenada next_point;
next_point.y = p.y;
next_point.x = p.x + 1;
queue.push(&queue, next_point);
}
}
Coordenada empty_coord;
/* Otherwise */
return empty_coord;
}
bool isFree( unsigned char ** grid, int8_t ** map, int y, int x, unsigned long height, unsigned long width )
{
if((y >= 0 && y < height) && (x >= 0 && y < width) && (map[y][x] == 0) && (grid[y][x] == 255))
return true;
return false;
}
/**
* Point in Matrix
*
* We use this data type to represent a point in our
* Euclidean Space Matrix.
*/
typedef struct
{
int x;
int y;
} Coordenada;
/**
* The Node struct,
* contains item and the pointer that point to next node.
*
* Ref: http://ben-bai.blogspot.com.br/2012/04/simple-queue-data-structure-in-ansi-c.html
*/
typedef struct Node {
Coordenada item;
struct Node* next;
} Node;
/**
* The Queue struct, contains the pointers that
* point to first node and last node, the size of the Queue,
* and the function pointers.
*/
typedef struct Queue {
Node* head;
Node* tail;
void (*push) (struct Queue*, Coordenada); // add item to tail
// get item from head and remove it from queue
Coordenada (*pop) (struct Queue*);
// get item from head but keep it in queue
Coordenada (*peek) (struct Queue*);
// display all element in queue
void (*display) (struct Queue*);
// size of this queue
int size;
} Queue;