C语言基础题:迷宫寻路(C语言版)

1.题目描述


机器猫被困在一个矩形迷宫里。
迷宫可以视为一个n x m 矩阵,每个位置要么是空地,要么是墙。机器猫只能从一个空地走到其上、下、左、右的空地。
机器猫初始时位于(1,1)的位置,问能否走到(n,m)位置。

2.输入格式


第一行,两个正整数 n,m。
接下来几行,输入这个迷宫。每行输入一个长为 m 的字符串,#表示墙,. 表示空地。

3.输出格式


仅一行,一个字符串。如果机器猫能走到(n,m),则输出 Yes;否则输出 No 。

4.输入输出样例

1.输入:
3 5
.##.#
.#...
...#.
2.输出:
Yes

5.说明/提示


样例解释
路线如下:(1,1)→(2,1)→(3,1)→(3,2)→(3,3)→(2,3)→(2,4)→(2,5)→(3,5)

数据规模与约定
对于 100% 的数据,保证1< n,m < 100,(1,1)和(n,m)均为空地。

代码:

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

#define MAXN 1000

typedef struct {
    int x, y;
} Point;

int n, m;
char maze[MAXN][MAXN + 1];
int visited[MAXN][MAXN]; // 访问标记
Point queue[MAXN * MAXN]; // 队列用于 BFS
int front = 0, rear = 0;

// 移动方向:上下左右
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

void enqueue(Point p) {
    queue[rear++] = p;
}

Point dequeue() {
    return queue[front++];
}

int is_valid(int x, int y) {
    return (x >= 0 && x < n && y >= 0 && y < m && maze[x][y] == '.' && !visited[x][y]);
}

int bfs() {
    enqueue((Point){0, 0}); // 从 (0,0) 开始
    visited[0][0] = 1; // 标记为已访问

    while (front < rear) {
        Point current = dequeue();
        
        // 如果到达终点 (n-1, m-1)
        if (current.x == n - 1 && current.y == m - 1) {
            return 1; // 可到达
        }

        // 检查四个方向
        for (int i = 0; i < 4; i++) {
            int new_x = current.x + dir[i][0];
            int new_y = current.y + dir[i][1];

            if (is_valid(new_x, new_y)) {
                visited[new_x][new_y] = 1; // 标记为已访问
                enqueue((Point){new_x, new_y}); // 入队
            }
        }
    }
    return 0; // 不可到达
}

int main() {
    scanf("%d %d", &n, &m);
    
    // 读取迷宫
    for (int i = 0; i < n; i++) {
        scanf("%s", maze[i]);
    }

    // 如果起点或终点是墙,直接输出 No
    if (maze[0][0] == '#' || maze[n-1][m-1] == '#') {
        printf("No\n");
        return 0;
    }

    // 执行 BFS
    if (bfs()) {
        printf("Yes\n");
    } else {
        printf("No\n");
    }

    return 0;
}

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用C++语言生成迷宫并自动寻路的示例代码: ```cpp #include <iostream> #include <vector> #include <stack> #include <cstdlib> #include <ctime> using namespace std; const int MAX_ROW = 20; const int MAX_COL = 20; const int WALL = 1; const int PATH = 0; struct Point { int row, col; Point(int r, int c) : row(r), col(c) {} }; // 随机生成一个迷宫 void generateMaze(vector<vector<int>>& maze) { srand(time(nullptr)); int rows = maze.size(); int cols = maze[0].size(); // 初始化迷宫所有位置为墙 for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { maze[i][j] = WALL; } } // 随机选择一个起始点 Point start(rand() % rows, rand() % cols); maze[start.row][start.col] = PATH; stack<Point> path; path.push(start); while (!path.empty()) { Point cur = path.top(); path.pop(); // 获取当前点的所有邻居 vector<Point> neighbors; if (cur.row > 1 && maze[cur.row - 2][cur.col] == WALL) { neighbors.emplace_back(Point(cur.row - 2, cur.col)); } if (cur.row < rows - 2 && maze[cur.row + 2][cur.col] == WALL) { neighbors.emplace_back(Point(cur.row + 2, cur.col)); } if (cur.col > 1 && maze[cur.row][cur.col - 2] == WALL) { neighbors.emplace_back(Point(cur.row, cur.col - 2)); } if (cur.col < cols - 2 && maze[cur.row][cur.col + 2] == WALL) { neighbors.emplace_back(Point(cur.row, cur.col + 2)); } // 随机选择一个邻居作为下一个点 if (!neighbors.empty()) { Point next = neighbors[rand() % neighbors.size()]; int wall_row = (cur.row + next.row) / 2; int wall_col = (cur.col + next.col) / 2; maze[wall_row][wall_col] = PATH; // 将中间的墙打通 maze[next.row][next.col] = PATH; // 将下一个点标记为路径 path.push(cur); path.push(next); } } } // 显示迷宫 void printMaze(const vector<vector<int>>& maze) { for (const auto& row : maze) { for (int cell : row) { cout << (cell == WALL ? "█" : " "); } cout << endl; } } // 深度优先搜索寻找通路 bool findPath(vector<vector<int>>& maze, Point start, Point end, vector<Point>& path) { static const int DIRECTIONS[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; path.clear(); stack<Point> s; s.push(start); maze[start.row][start.col] = WALL; while (!s.empty()) { Point cur = s.top(); s.pop(); path.push_back(cur); if (cur.row == end.row && cur.col == end.col) { return true; } for (const auto& dir : DIRECTIONS) { int r = cur.row + dir[0]; int c = cur.col + dir[1]; if (r < 0 || r >= maze.size() || c < 0 || c >= maze[0].size()) { continue; } if (maze[r][c] == PATH) { maze[r][c] = WALL; s.push(Point(r, c)); } } } return false; } int main() { vector<vector<int>> maze(MAX_ROW, vector<int>(MAX_COL)); generateMaze(maze); printMaze(maze); Point start(0, 0); Point end(MAX_ROW - 1, MAX_COL - 1); vector<Point> path; if (findPath(maze, start, end, path)) { cout << "Path found:" << endl; for (const auto& p : path) { cout << "(" << p.row << ", " << p.col << ")" << endl; } } else { cout << "No path found!" << endl; } return 0; } ``` 该程序首先使用深度优先搜索算法生成一个迷宫,然后使用同样的算法寻找从左上角到右下角的通路并输出路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

头顶一只喵喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值