题解 | #走迷宫#

走迷宫

https://www.nowcoder.com/practice/e88b41dc6e764b2893bc4221777ffe64

#include <iostream>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
char g[N][N];
int d[N][N];
int x1, y1, x2, y2;
int n, m;
int bfs(){
    queue<PII> q;
    memset(d, -1, sizeof d);
    d[x1][y1] = 0;
    q.push({x1, y1});
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
    while(q.size()){
        auto t = q.front();
        q.pop();
        for(int i = 0; i < 4; i ++){
            int x = t.first + dx[i], y = t.second + dy[i];
            if(x < n && y < m && x >= 0 && y >= 0 && g[x][y] == '.' && d[x][y] == -1){
                d[x][y] = d[t.first][t.second] + 1;
                q.push({x, y});
            }
        }
    }
    return d[x2][y2];
}

int main() {
    cin >> n >> m;
    cin >> x1 >> y1 >> x2 >> y2;
    x1 --, y1 --, x2 --, y2 --;
    for(int i = 0; i < n; i ++)
        for(int j = 0; j < m; j ++)
            cin >> g[i][j];
    cout << bfs() << endl;
    return 0;
}

全部评论

相关推荐

06-26 15:35
武汉大学 运营
点赞 评论 收藏
分享
点赞 评论 收藏
分享
湫湫湫不会java:写的很杂,连自己都不知道找什么工作的感觉,只是要份工作。针对自己稍微有点优势的方向好好整份简历投投吧,然后这杂的简历就辅助投投,因为自己认为的优势可能也不是很大的优势all in可能失业,自己也没有啥很想的方向还是可以用这通用的碰碰运气吧,加油
点赞 评论 收藏
分享
仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务