博客
关于我
迷宫寻路 (20 分)
阅读量:670 次
发布时间:2019-03-15

本文共 3200 字,大约阅读时间需要 10 分钟。

为了解决这个问题,我们需要找到从迷宫左上角(1,1)到右下角(5,8)的最短路径。迷宫中使用0表示通路,1表示障碍物。我们可以使用广度优先搜索(BFS)来确保找到最短路径。

方法思路

  • 输入处理:读取迷宫的行数和列数,然后读取迷宫的具体结构。
  • 检查起点:确保起点(1,1)是通路,否则直接输出“NO FOUND”。
  • BFS初始化:使用队列进行BFS,记录当前路径,避免重复访问同一个点。
  • 路径记录:每次访问一个点时,记录路径,并将未访问的邻居加入队列。
  • 终点检查:如果在BFS过程中找到终点(5,8),输出路径;否则,输出“NO FOUND”。
  • 解决代码

    #include 
    #include
    #include
    #include
    using namespace std;
    // 定义常量
    const int N = 110;
    const int INF = 0x3f3f3f3f;
    int dist[N][2], path[N][2];
    int m, n;
    bool visited[N+1][N+1];
    // 四个方向:上下左右
    int dx[] = {0, 1, 0, -1};
    int dy[] = {1, 0, -1, 0};
    void bfs() {
    queue
    > q; vector
    step; q.push(make_pair(1, 1)); step.clear(); dist[0][0] = 1; path[0][0] = make_pair(1, 1); if (1 == m && 1 == n) { // 起点就是终点 dist[0][0] = 0; path[0][0] = make_pair(1, 1); return; } int found = 0; while (!q.empty()) { auto current = q.front(); q.pop(); int x = current.first, y = current.second; step.push_back(current); if (x == m && y == n) { found = 1; break; } for (int i = 0; i < 4; ++i) { int tx = x + dx[i]; int ty = y + dy[i]; if (tx >= 1 && tx <= n && ty >= 1 && ty <= m) { if (!visited[tx][ty] && g[tx][ty] == 0) { visited[tx][ty] = 1; dist[step.size() - 1][0] = tx; dist[step.size() - 1][1] = ty; path[step.size() - 1][0] = tx; path[step.size() - 1][1] = ty; q.push(make_pair(tx, ty)); } } } } if (found) { for (int i = 0; i < step.size(); ++i) { int x = path[i].first; int y = path[i].second; // 转换为题目要求的1-based索引 cout << x << ","; cout << y << endl; } } else { cout << "NO FOUND" << endl; } } int main() { while (true) { cin >> n >> m; if (m == -1) { break; } // 读取迷宫数据 vector
    > g(m + 1, vector
    (n + 1, 0)); for (int i = 1; i <= m; ++i) { string line; while (line.empty()) { getline(cin, line); } vector
    row; istringstream iss(line); while (row.size() < n) { int num; iss >> num; row.push_back(num); } for (int j = 1; j <= n; ++j) { g[i][j] = row[j - 1]; } } // 检查起点是否是通路 if (g[1][1] != 0) { cout << "NO FOUND" << endl; continue; } // 初始化BFS visited = {{false}}; vector
    steps; queue
    > q; q.push(make_pair(1, 1)); visited[1][1] = true; if (1 == m && 1 == n) { cout << "1,1" << endl; continue; } bool success = false; // BFS过程 while (!q.empty()) { auto current = q.front(); q.pop(); int x = current.first, y = current.second; for (int i = 0; i < 4; ++i) { int tx = x + dx[i]; int ty = y + dy[i]; if (tx >= 1 && tx <= n && ty >= 1 && ty <= m) { if (!visited[tx][ty] && g[tx][ty] == 0) { visited[tx][ty] = true; if (tx == m && ty == n) { success = true; break; } // 记录路径 if (steps.empty()) { dist[0][0] = tx; dist[0][1] = ty; path[0][0] = make_pair(tx, ty); steps.push_back(0); } else { dist[steps.size()][0] = tx; dist[steps.size()][1] = ty; path[steps.size()][0] = make_pair(tx, ty); steps.push_back(steps.size()); } q.push(make_pair(tx, ty)); } } } if (success) { break; } } if (success) { for (int i = 0; i < steps.size(); ++i) { int x = path[i].first; int y = path[i].second; // 转换为题目要求的1-based索引 cout << x << ","; cout << y << endl; } } else { cout << "NO FOUND" << endl; } } return 0; }

    代码解释

  • 读取输入:程序读取迷宫的行数和列数,接着读取每行的迷宫数据。
  • BFS初始化:检查起点是否为通路,初始化队列和路径记录数组。
  • BFS执行:从起点开始,逐层扩展,检查四个方向的邻居是否为通路且未访问过。
  • 路径记录:每次访问一个点时,记录路径,并将邻居加入队列。
  • 终点检查:如果找到终点,输出路径;否则,输出“NO FOUND”。
  • 转载地址:http://pvzqz.baihongyu.com/

    你可能感兴趣的文章
    NI笔试——大数加法
    查看>>
    NLog 自定义字段 写入 oracle
    查看>>
    NLog类库使用探索——详解配置
    查看>>
    NLP 基于kashgari和BERT实现中文命名实体识别(NER)
    查看>>
    NLP 模型中的偏差和公平性检测
    查看>>
    Vue3.0 性能提升主要是通过哪几方面体现的?
    查看>>
    NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
    查看>>
    NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
    查看>>
    NLP三大特征抽取器:CNN、RNN与Transformer全面解析
    查看>>
    NLP学习笔记:使用 Python 进行NLTK
    查看>>
    NLP度量指标BELU真的完美么?
    查看>>
    NLP的不同研究领域和最新发展的概述
    查看>>
    NLP的神经网络训练的新模式
    查看>>
    NLP采用Bert进行简单文本情感分类
    查看>>
    NLP问答系统:使用 Deepset SQUAD 和 SQuAD v2 度量评估
    查看>>
    NLP项目:维基百科文章爬虫和分类【02】 - 语料库转换管道
    查看>>
    NLP:使用 SciKit Learn 的文本矢量化方法
    查看>>
    nmap 使用方法详细介绍
    查看>>
    Nmap扫描教程之Nmap基础知识
    查看>>
    nmap指纹识别要点以及又快又准之方法
    查看>>