I am trying to implement BFS to find the shortest path from source to target in a maze.
The problem I am facing is that I cannot print the path, it is printed with "*" in the maze, but how can I extract the path from the BFS predecessors without printing all the nodes visited?
Here is my compilation code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct coord{
int row;
int col;
};
struct TQueue{
struct coord *A;
int QUEUE_MAX;
};
typedef struct TQueue *Queue;
Queue initQueue(int size){
Queue Q = malloc(sizeof(struct TQueue));
Q->A = malloc(size*sizeof(struct coord));
Q->QUEUE_MAX = size+1;
Q->A[0].row = 0;
Q->A[Q->QUEUE_MAX].row = 1;
return Q;
}
int emptyQueue(Queue Q){
return Q->A[0].row == 0;
}
int fullQueue(Queue Q){
return Q->A[0].row == Q->A[Q->QUEUE_MAX].row;
}
void enqueue(Queue Q, struct coord value){
if(!fullQueue(Q)){
Q->A[Q->A[Q->QUEUE_MAX].row] = value;
if(emptyQueue(Q)){
Q->A[0].row = 1;
}
Q->A[Q->QUEUE_MAX].row = (Q->A[Q->QUEUE_MAX].row%(Q->QUEUE_MAX - 1)) + 1;
} else{
puts("Coda piena");
}
}
struct coord dequeue(Queue Q){
struct coord value;
if(!emptyQueue(Q)){
value = Q->A[Q->A[0].row];
Q->A[0].row = (Q->A[0].row%(Q->QUEUE_MAX - 1)) + 1;
if(fullQueue(Q)){
Q->A[0].row = 0;
Q->A[Q->QUEUE_MAX].row = 1;
}
} else{
puts("Coda piena");
}
return value;
}
struct TGraph{
char **nodes;
int rows;
int cols;
struct coord S;
struct coord T;
};
typedef struct TGraph *Graph;
enum color{
WHITE, GREY, BLACK
};
int BFSPathMatrix(Graph G, struct coord *pred){
int i, j;
struct coord neighbor, source = G->S;
enum color visited[G->rows][G->cols];
for(i = 0; i < G->rows; i++)
for(j = 0; j < G->cols; j++)
visited[i][j] = WHITE;
Queue coda = initQueue(G->rows*G->cols);
visited[G->S.row][G->S.col] = GREY;
enqueue(coda, source);
i = 0;
while(!emptyQueue(coda)){
source = dequeue(coda);
int moves[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
for(j = 0; j < 4; j++){
neighbor.row = source.row + moves[j][0];
neighbor.col = source.col + moves[j][1];
if(neighbor.row < 0 || neighbor.row >= G->rows || neighbor.col < 0 || neighbor.col >= G->cols)
continue;
if(neighbor.row == G->T.row && neighbor.col == G->T.col){
pred[i++] = G->T;
free(coda);
return 1;
}
if(visited[neighbor.row][neighbor.col] == WHITE && G->nodes[neighbor.row][neighbor.col] == ' '){
pred[i++] = source;
visited[neighbor.row][neighbor.col] = GREY;
enqueue(coda, neighbor);
}
}
}
free(coda);
return -1;
}
Graph initGraphMatrix(int rows, int cols){
int i;
Graph G = malloc(sizeof(struct TGraph));
G->nodes = malloc(rows*sizeof(char *));
for(i = 0; i < rows; i++)
G->nodes[i] = malloc(cols*sizeof(char));
G->rows = rows;
G->cols = cols;
return G;
}
void printGraphMatrix(Graph G){
if(G != NULL){
int i, j;
for(i = 0; i < G->rows; i++){
for(j = 0; j < G->cols; j++)
putchar(G->nodes[i][j]);
putchar('\n');
}
}
}
int main(){
Graph G = initGraphMatrix(11, 21);
G->S.row = 1;
G->S.col = 1;
G->T.row = 9;
G->T.col = 7;
strcpy(G->nodes[0], "|-------------------|");
strcpy(G->nodes[1], "|S | | |");
strcpy(G->nodes[2], "| |-| |-| |---| | | |");
strcpy(G->nodes[3], "| | | | | | |");
strcpy(G->nodes[4], "| | |---| | |---| | |");
strcpy(G->nodes[5], "| | | | | | | | |");
strcpy(G->nodes[6], "| | | | |-| | | | | |");
strcpy(G->nodes[7], "| | | | | | | |");
strcpy(G->nodes[8], "| | | |-| |-------| |");
strcpy(G->nodes[9], "| | T| |");
strcpy(G->nodes[10], "|-------------------|");
struct coord pred[(G->rows*G->cols)];
printGraphMatrix(G);
system("PAUSE");
if(BFSPathMatrix(G, pred) != -1){
int i;
for(i = 0; i < G->rows*G->cols; i++){
if(pred[i].row == G->S.row && pred[i].col == G->S.col)
continue;
if(pred[i].row == G->T.row && pred[i].col == G->T.col)
break;
G->nodes[pred[i].row][pred[i].col] = '*';
}
printGraphMatrix(G);
}else
puts("Target unreachable");
system("PAUSE");
return 0;
}
This is how the maze before and after BFS looks like:

How to print only the shortest path? And why doesn't space before 'T' have '*' in it? Thank you in advance for all your time.