凯H 2023-06-07 21:00 采纳率: 81%
浏览 87
已结题

解决迷宫问题中无法运行的问题

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 20  // 迷宫行数
#define COL 20  // 迷宫列数

typedef struct {
    int row;  // 行号
    int col;  // 列号
} Point;

typedef struct {
    Point data[ROW * COL];  // 存放队列元素的数组
    int front;  // 队头指针
    int rear;  // 队尾指针
} Queue;

// 初始化队列
void initQueue(Queue *queue) {
    queue->front = 0;
    queue->rear = 0;
}

// 判断队列是否为空
int isQueueEmpty(Queue *queue) {
    return queue->front == queue->rear;
}

// 入队操作
void enqueue(Queue *queue, Point point) {
    queue->data[queue->rear++] = point;
}

// 出队操作
Point dequeue(Queue *queue) {
    return queue->data[queue->front++];
}
int findPath(int maze[][COL]);

// 随机生成迷宫
void generateMaze(int maze[][COL]) {
    int i, j;
    srand(time(NULL));  // 初始化随机数种子
    // 初始化迷宫
    for (i = 0; i < ROW; i++) {
        for (j = 0; j < COL; j++) {
            if (i == 0 || j == 0 || i == ROW - 1 || j == COL - 1) {
                // 周围一圈设置为墙
                maze[i][j] = 1;
            } else {
                // 其他位置随机设置为墙或路径
                maze[i][j] = rand() % 2;
            }
        }
    }
    // 确保迷宫至少存在一条路径
    while (1) {
        int maze_copy[ROW][COL];
        for (i = 0; i < ROW; i++) {
            for (j = 0; j < COL; j++) {
                maze_copy[i][j] = maze[i][j];
            }
        }
        if (findPath(maze_copy)) {
            break;
        } else {
            for (i = 1; i < ROW - 1; i++) {
                for (j = 1; j < COL - 1; j++) {
                    maze[i][j] = rand() % 2;
                }
            }
        }
    }
}
 // 打印迷宫
void printMaze(int maze[][COL]) {
    int i, j;
    for (i = 0; i < ROW; i++) 
    {
        for (j = 0; j < COL; j++) 
        {
            if (maze[i][j] == 1) 
            {
                system("color FC"); 
                printf("#");
            } else if (maze[i][j] == 2) 
            {
                // 路径用绿色表示
                system("color FA");
                printf("@");
            } else 
            {
                // 其他位置用白色表示
                printf(" ");
            }
        }
        printf("\n");
    }
}

// 判断是否为有效位置
int isValid(int maze[][COL], int row, int col) {
    return row >= 0 && row < ROW && col >= 0 && col < COL && (maze[row][col] == 0 || maze[row][col] == 2);
}
// 广度优先遍历寻找路径
int findPath(int maze[][COL]) {
    Queue queue;
    Point start = {1, 0};  // 起点
    Point end = {ROW - 2, COL - 1};  // 终点
    Point curr, next;
    int i, row, col;
    int visited[ROW][COL] = {0};  // 记录是否已经访问过
    initQueue(&queue);
    enqueue(&queue, start);
    visited[start.row][start.col] = 1;
    while (!isQueueEmpty(&queue)) {
        curr = dequeue(&queue);
        if (curr.row == end.row && curr.col == end.col) {
            // 已到达终点
            maze[curr.row][curr.col] = 2;
            return 1;
        }
        // 向上走
        row = curr.row - 1;
        col = curr.col;
        if (isValid(maze, row, col) && !visited[row][col]) {
            next.row = row;
            next.col = col;
            enqueue(&queue, next);
            visited[row][col] = 1;
        }
        // 向下走
        row = curr.row + 1;
        col = curr.col;
        if (isValid(maze, row, col) && !visited[row][col]) {
            next.row = row;
            next.col = col;
            enqueue(&queue, next);
            visited[row][col] = 1;
        }
        // 向左走
        row = curr.row;
        col = curr.col - 1;
        if (isValid(maze, row, col) && !visited[row][col]) {
            next.row = row;
            next.col = col;
            enqueue(&queue, next);
            visited[row][col] = 1;
        }
        // 向右走
        row = curr.row;
        col = curr.col + 1;
        if (isValid(maze, row, col) && !visited[row][col]) {
            next.row = row;
            next.col = col;
            enqueue(&queue, next);
            visited[row][col] = 1;
        }
        // 标记当前位置已经访问过
        maze[curr.row][curr.col] = 2;
    }
    return 0;
}

int main() {
    int maze[ROW][COL];
    generateMaze(maze);
    printf("迷宫:\n");
    printMaze(maze);
    if (findPath(maze)) {
        printf("找到了一条路径:\n");
        printMaze(maze);
    } else {
        printf("没有找到路径。\n");
    }
    return 0;
}

运行后只出现黑框,显示无错误,无警告,咋办

img

  • 写回答

10条回答 默认 最新

  • 终泪 2023-06-08 16:26
    关注
    获得0.45元问题酬金

    需要注意的是,该程序的输出界面使用了Windows系统下的控制台颜色设置,可能在其他操作系统和终端下无法正常显示颜色。可以尝试修改程序中使用控制台颜色的部分,在其他操作系统和终端下以不同的方式展示路径和墙壁。比如,可以使用 ASCII 字符集中的字符代替颜色,或者使用 ANSI Escape Codes 来设置文本颜色和背景色等。另外,也可以考虑使用图形界面来展示迷宫和路径,这样可以避免因为终端上的限制而产生的不便。

    评论

报告相同问题?

问题事件

  • 系统已结题 6月15日
  • 创建了问题 6月7日

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?