华为机试2

本文介绍了一个使用C++实现的迷宫寻路算法,通过广度优先搜索(BFS)来寻找从起点到终点的路径。该算法能够处理任意大小的迷宫地图,并在存在通路的情况下找到通往目标的路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//

//  main.cpp

//  C++

//

//  Created by 胡平伍 on 16/3/30.

//  Copyright © 2016 胡平伍. All rights reserved.

//


//const static char * path = "/Users/pw/iOS程序代码/C++/C++/input.txt";


#include <iostream>

#include <string>

#include <vector>

#include <queue>

using namespace std;



int main(int argc, const char * argv[]) {

    int r, c;

    while (cin >> r >> c) {

        vector<string> v;

        string tmp;

        for (int i = 0; i < r; ++ i) {

            cin >> tmp;

            v.push_back(tmp);

        }

        

        int m, n; // B的位置

        for (int i = 0; i < v.size(); ++ i) {

            for (int j = 0; j < v[i].length(); ++ j) {

                if (v[i][j] == 'B') {

                    m = i;

                    n = j;

                }

            }

        }

        

        queue<pair<int, int>> que;

        que.push(make_pair(m, n));

        pair<int, int> cur;

        bool flag = false;

        while (!que.empty()) {

            cur = que.front();

            que.pop();

            int x = cur.first, y = cur.second;

//            cout << "x = " << x << ", y = " << y << endl;

            if (x > 0) {

                if (v[x - 1][y] == '-') {

//                    cout << "\t" << x - 1 << ", " << y << endl;

                    que.push(make_pair(x - 1, y));

                    v[x - 1][y] = '#';

                } else if (v[x - 1][y] == 'H') {

//                    cout << 'Y' << endl;

                    flag = true;

                    break;

                }

            }

            if (x < r - 1) {

                if (v[x + 1][y] == '-') {

//                    cout << "\t" << x + 1 << ", " << y << endl;

                    que.push(make_pair(x + 1, y));

                    v[x + 1][y] = '#';

                } else if (v[x + 1][y] == 'H') {

//                    cout << 'Y' << endl;

                    flag = true;

                    break;

                }

            }

            if (y > 0) {

                if (v[x][y - 1] == '-') {

//                    cout << "\t" << x << ", " << y - 1 << endl;

                    que.push(make_pair(x, y - 1));

                    v[x][y - 1] = '#';

                } else if (v[x][y - 1] == 'H') {

//                    cout << 'Y' << endl;

                    flag = true;

                    break;

                }

            }

            if (y < c - 1) {

                if (v[x][y + 1] == '-') {

//                    cout << "\t" << x << ", " << y + 1 << endl;

                    que.push(make_pair(x, y + 1));

                    v[x][y + 1] = '#';

                } else if (v[x][y + 1] == 'H') {

//                    cout << 'Y' << endl;

                    flag = true;

                    break;

                }

            }

        }

        if (flag) {

            cout << "Y" << endl;

        } else {

            cout << "N" << endl;

        }

//        if (que.empty() && v[cur.first][cur.second] != 'H') {

//            cout << 'N' << endl;

//        }

    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值