0

Evening, I think misunderstanding how bfs works in a maze solver. I trying to figure out this error originating from my enqueue function. Did I allocate the array incorrectly?

It may be design flaw with my maze solver. Am I accessing a possible block not allocated?

I am also finding problems with prev[]. I believe I need to redesign it as C90 prompts me a warning.

enter image description here

struct queue_t *queue_create(int size)
{
    struct queue_t *queue = malloc(sizeof(struct queue_t));
    queue->array = malloc(sizeof(struct coordinate_t) * size + 1);
    queue->capacity = size;
    queue->size = size;
    queue->array[0].coord_row = 0;
    queue->array[0].coord_col = 0;
    queue->array[queue->capacity].coord_row = 1;
    queue->array[queue->capacity].coord_col = 1;
    return queue;
}

void enqueue(struct queue_t *queue, struct coordinate_t coordinate)
{
    if (!queue_full(queue)) {
        queue->array[queue->array[queue->capacity].coord_row] = coordinate;
        queue->array[queue->capacity].coord_row = (queue->array[queue->capacity].coord_row + 1) % queue->capacity;

        if (queue_empty(queue)) {
            queue->array[0].coord_row = 1;
        }


    } else {
        printf("\nQueue is full");
    }
}

int main(int argc, char *argv[])
{
    if (argc == 2) {
        int row_map_size;
        int col_map_size;

        int start_row;
        int start_col;
        int goal_row;
        int goal_col;

        map_input(argv, &row_map_size, &col_map_size, &start_row,
              &start_col, &goal_row, &goal_col);


        FILE *fp = fopen(argv[1], "r");
        rewind(fp);

        if (!fp) {
            printf("\nFILE ERROR: Unable to open %s", argv[1]);
            printf("\nExiting...");
            exit(EXIT_FAILURE);
        }

        struct graph_t *graph = graph_create(row_map_size,
            col_map_size);

        graph->start.coord_row = start_row;
        graph->start.coord_col = start_col;
        graph->goal.coord_row = goal_row;
        graph->goal.coord_col = goal_col;

        char buffer[MAX_BUFFER];

        for (int i = 0; i < row_map_size; ++i) {
            fgets(buffer, MAX_BUFFER, fp);
            buffer[strlen(buffer) - 1] = '\0';
            strcpy(graph->adj_nodes[i], buffer);
        }

        int size = graph->rows * graph->columns;
        struct coordinate_t prev[size];

        graph_print(graph);

        int bfs = graph_bfs_path(graph, prev);

        if (bfs != -1) {
            struct coordinate_t goal = graph->goal;
            int prev_idx = goal.coord_row * graph->columns +
                goal.coord_col;

            printf("\nprev_idx: %d", prev_idx);

            while (prev[prev_idx].coord_row !=
                graph->start.coord_row
                || prev[prev_idx].coord_col !=
                    graph->start.coord_col) {

                prev_idx = goal.coord_row * graph->columns +
                    goal.coord_col;

                goal = prev[prev_idx];

                if (graph->adj_nodes[goal.coord_row]
                [goal.coord_col] == ' ') {
                    graph->adj_nodes[goal.coord_row]
                    [goal.coord_col] = '.';
                }
            }

            graph_print(graph);

            fclose(fp);
            free_graph(graph);

        } else {
            printf("\nFILE ERROR: Can't find a path");
            graph_print(graph);
            free_graph(graph);
        }

    } else {
        printf("\nUSAGE ERROR: %s [filename].txt", argv[0]);
        printf("\nExiting...");
        exit(EXIT_FAILURE);
    }

    return 0;
}

0 Answers0