#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;
}
运行后只出现黑框,显示无错误,无警告,咋办