我试图在C中实现广度优先搜索算法,以搜索从二进制矩阵中的第一个(第一列)白色(255)点到最后一个(最后一列)的路径。
我已经创建了一种基于直方图正确地对图像进行二值化的算法,但是现在当我试图找到最短路径时,我遇到了问题。也许我不太了解该怎么做。
我是grid
,这是一个包含二进制值(0或255)的矩阵,其中255表示我可以" walk",0表示它" 39; sa" wall"。而且我是map
,知道我可以去哪里,以及我去过哪里。两者都有相同的尺寸。
我将粘贴BFS算法的代码,并在Gist中插入所有代码,如果你想查看,或者为某些人的进一步参考。
视频:Coordenada
表示坐标。
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;
Gist中的链接: Gist containing Sources
感谢阅读(:
更多参考资料,Github Repository。