B - Find a way

双向bfs
还是比较水的。
用vector记录下来每一个KCF
然后每次bfs都判断一个是否有哪些点是2个人都到达的点,直接输出答案就可

#include<bits/stdc++.h>
using namespace std;

int n, m;
int Map[205][205];
int flagY[205][205];
int flagM[205][205];
int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};

struct node {
    int x, y;
    int t;
};

vector<node> vec;
queue<node> qy;
queue<node> qm;

int main() {

    while(cin >> n >> m) {
        string str;
        int ans = 999999;
        vec.clear();
        while(qy.size()) qy.pop();
        while(qm.size()) qm.pop();
        memset(flagY, 0, sizeof flagY);
        memset(flagM, 0, sizeof flagM);
        for(int i = 0; i < n; i++) {
            cin >> str;
            for(int j = 0; j < str.size(); j++) {
                if(str[j] == 'M') {
                    node tmp;
                    tmp.x = i;
                    tmp.y = j;
                    tmp.t = 0;
                    Map[i][j] = -1;
                    qm.push(tmp);
                }
                if(str[j] == 'Y') {
                    node tmp;
                    tmp.x = i;
                    tmp.y = j;
                    tmp.t = 0;
                    Map[i][j] = -1;
                    qy.push(tmp);
                }
                if(str[j] == '@') {
                    Map[i][j] = 2;
                    node tmp;
                    tmp.x = i;
                    tmp.y = j;
                    vec.push_back(tmp);
                }
                if(str[j] == '#') {
                    Map[i][j] = 1;
                }
                if(str[j] == '.') {
                    Map[i][j] = 0;
                }
            }
        }
        while(qy.size() || qm.size()) {
            node ty, tm;
            if(qy.size()) {
                ty = qy.front();
                qy.pop();
                for(int i = 0; i < 4; i++) {
                    if(ty.x + dir[i][0] >=0 && ty.x + dir[i][0] < n && ty.y + dir[i][1] >= 0 && ty.y + dir[i][1] < m) {
                        node tmp;
                        tmp.x = ty.x + dir[i][0];
                        tmp.y = ty.y + dir[i][1];
                        tmp.t = ty.t + 1;
                        if(flagY[tmp.x][tmp.y] == 0 && Map[tmp.x][tmp.y] != 1 && Map[tmp.x][tmp.y] != -1) {
                            flagY[tmp.x][tmp.y] = tmp.t;
                            qy.push(tmp);
                        }
                    }
                }
            }
            if(qm.size()) {
                tm = qm.front();
                qm.pop();
                for(int i = 0; i < 4; i++) {
                    if(tm.x + dir[i][0] >=0 && tm.x + dir[i][0] < n && tm.y + dir[i][1] >= 0 && tm.y + dir[i][1] < m) {
                        node tmp;
                        tmp.x = tm.x + dir[i][0];
                        tmp.y = tm.y + dir[i][1];
                        tmp.t = tm.t + 1;
                        if(flagM[tmp.x][tmp.y] == 0 && Map[tmp.x][tmp.y] != 1 && Map[tmp.x][tmp.y] != -1) {
                            flagM[tmp.x][tmp.y] = tmp.t;
                            qm.push(tmp);
                        }

                    }
                }
            }
            for(int i = 0; i < vec.size(); i++) {
                if(flagM[vec[i].x][vec[i].y] != 0 && flagY[vec[i].x][vec[i].y] != 0) {
                    ans = min(ans, flagM[vec[i].x][vec[i].y] + flagY[vec[i].x][vec[i].y]);
                }
            }
        }
        cout << ans * 11<< endl;

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值