败降_ 2024-04-29 16:13 采纳率: 0%
浏览 4

C语言广度优先搜索洛谷

为什么在oj中显示Program exited with code 1.??真的百思不得其解,改了好多遍了,求指点!!


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX_NODES 100

struct Tree {
    int number;
    bool visited;
    struct Tree** child;
    int count;
};

struct Queue {
    struct Tree* items[MAX_NODES];
    int front;
    int rear;
};

void initQueue(struct Queue* q) {
    q->front = 0; // Initialize front to 0
    q->rear = 0; // Initialize rear to 0
}

void enqueue(struct Queue* q, struct Tree* node) {
    if ((q->rear + 1) % MAX_NODES == q->front) { // Check if queue is full
        printf("Queue is full\n");
        exit(EXIT_FAILURE);
    }

    q->rear = (q->rear + 1) % MAX_NODES; // Move rear circularly
    q->items[q->rear] = node;
    node->visited = true;
}

struct Tree* dequeue(struct Queue* q) {
    if (q->front == q->rear) { // Check if queue is empty
        printf("Queue is empty\n");
        exit(EXIT_FAILURE);
    }
    q->front = (q->front + 1) % MAX_NODES; // Move front circularly
    struct Tree* item = q->items[q->front];
    return item;
}

void articledfs(struct Tree* node) {
    if (node == NULL || node->visited) {
        return;
    }

    printf("%d ", node->number);
    node->visited = true;

    for (int i = 0; i < node->count; i++) {
        articledfs(node->child[i]);
    }
}

void articlebfs(struct Tree* root) {
    struct Queue q;
    initQueue(&q);
    enqueue(&q, root);

    while (q.front != q.rear) {
        struct Tree* current = dequeue(&q);
        printf("%d ", current->number);

        for (int i = 0; i < current->count; i++) {
            struct Tree* childNode = current->child[i];
            if (!childNode->visited) {
                enqueue(&q, childNode);
                childNode->visited = true;
            }
        }
    }
}

int main() {
    int n, m;
    scanf("%d%d", &n, &m);

    struct Tree* nodes = (struct Tree*)malloc(n * sizeof(struct Tree));
    if (nodes == NULL) {
        printf("Memory allocation failed\n");
        return -1;
    }

    for (int i = 0; i < n; i++) {
        nodes[i].number = i + 1;
        nodes[i].visited = false;
        nodes[i].count = 0;
        nodes[i].child = (struct Tree**)malloc(MAX_NODES * sizeof(struct Tree*));
        if (nodes[i].child == NULL) {
            printf("Memory allocation failed\n");
            return -1;
        }
    }

    for (int i = 0; i < m; i++) {
        int pre, number;
        scanf("%d%d", &pre, &number);
        pre--;

        if (pre < 0 || pre >= n || number < 1 || number > n) {
            printf("Invalid node numbers\n");
            return -1;
        }

        nodes[pre].child[nodes[pre].count++] = &nodes[number - 1];
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < nodes[i].count; j++) {
            for (int k = j + 1; k < nodes[i].count; k++) {
                if (nodes[i].child[j]->number > nodes[i].child[k]->number) {
                    struct Tree* temp = nodes[i].child[j];
                    nodes[i].child[j] = nodes[i].child[k];
                    nodes[i].child[k] = temp;
                }
            }
        }
    }

    for (int i = 0; i < n; i++) {
        articledfs(&nodes[i]);
    }
    printf("\n");

    for (int i = 0; i < n; i++) {
        nodes[i].visited = false;
    }

    articlebfs(&nodes[0]);

    for (int i = 0; i < n; i++) {
        free(nodes[i].child);
    }
    free(nodes);

    return 0;
}

  • 写回答

5条回答 默认 最新

  • 码农阿豪@新空间 新星创作者: 前端开发技术领域 2024-04-29 16:13
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    这个错误通常表示程序在某个位置崩溃或出现了错误,但是并没有输出特定的错误信息。可能会有多种原因导致这个错误,如内存泄漏、越界访问等,需要检查程序的逻辑和代码实现。 针对这个程序,我们可以通过以下步骤来查找问题:
    1. 检查变量和数据类型是否正确,是否有未初始化或未赋值的变量。
    2. 检查内存分配是否正确,是否有内存泄漏或越界访问的情况。
    3. 检查函数调用是否正确,是否有函数缺失或多余的情况。
    4. 检查逻辑是否正确,是否有死循环或死代码的情况。
    5. 检查输入输出是否正确,是否有输入不足或输出不完整的情况。 以下是一个可能导致错误的代码片段: nodes[i].child = (struct Tree**)malloc(MAX_NODES * sizeof(struct Tree*)); 这段代码没有检查malloc是否成功分配内存,如果分配失败,后面的程序就会出现错误。 以下是修改后的代码: nodes[i].child = (struct Tree**)malloc(MAX_NODES * sizeof(struct Tree*)); if (nodes[i].child == NULL) { // add error handling printf("Memory allocation failed\n"); return -1; } 这样修改之后,程序就会正确的处理内存分配失败的情况了。 需要注意的是,这只是一个可能存在问题的代码片段,实际问题可能还需要根据错误信息和代码逻辑来具体分析。因此,在解决这种问题时,需要认真分析程序的执行流程和错误信息,逐步剔除问题,找到问题的根本原因并解决。
    评论

报告相同问题?

问题事件

  • 创建了问题 4月29日