[kuangbin带你飞]专题一 简单搜索 N

N - Find a way

Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
Sample Input

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#

Sample Output
66
88
66

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int cnt[4][2]={1,0,-1,0,0,1,0,-1};
bool book[201][201];
char ans[201][201];
int n,m,sum;
int cur1[201][201],cur2[201][201];
struct Node {
    int x, y;
    int num;
    friend bool operator < (Node a ,Node b) {
        return a.num>b.num;
    }
};

void bfs(int flag,int fx,int fy) {
    memset(book,false,sizeof(book));
    book[fx][fy]=true;
    Node now,next;
    now.x=fx,now.y=fy,now.num=0;
    priority_queue<Node>Q;
    while(!Q.empty()) Q.pop();
    Q.push(now);
    while(!Q.empty()) {
        now=Q.top();
        Q.pop();
        if(ans[now.x][now.y]=='@') {
            if(flag==0) cur1[now.x][now.y]=now.num;
            else cur2[now.x][now.y]=now.num;
        }
        for(int i=0;i<4;i++) {
            next.x=now.x+cnt[i][0],next.y=now.y+cnt[i][1],next.num=now.num+1;
            if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&book[next.x][next.y]==false&&ans[next.x][next.y]!='#') {
                book[next.x][next.y]=true;
                Q.push(next);
            }
        }
    }
    return ;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    while(cin>>n>>m) {
        sum=INF;
        int tx,ty,fx,fy;
        memset(ans,0,sizeof(ans));
        memset(cur1,0,sizeof(cur1));
        memset(cur2,0,sizeof(cur2));
        for(int i=0;i<n;i++) {
            for(int j=0;j<m;j++) {
                cin>>ans[i][j];
                if(ans[i][j]=='Y') tx=i,ty=j;
                else if(ans[i][j]=='M') fx=i,fy=j;
            }
        }
        bfs(0,fx,fy);
        bfs(1,tx,ty);
        for(int i=0;i<n;i++) {
            for(int j=0;j<m;j++) {
                if(ans[i][j]=='@'&&cur1[i][j]!=0&&cur2[i][j]!=0) {
                    sum=min(sum,cur1[i][j]+cur2[i][j]);
                }
            }
        }
        cout<<sum*11<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
并查集是一种常用的数据结构,用于管理一个不相交集合的数据。在并查集中,每个元素都有一个父节点指向它所属的集合的代表元素。通过查找和合并操作,可以判断两个元素是否属于同一个集合,并将它们合并到同一个集合中。 在解决某些问题时,可以使用并查集进行优化。例如,在区间查询中,可以通过优化并查集的查询过程,快速找到第一个符合条件的点。 对于拆边(destroy)操作,一般的并查集无法直接实现这个功能。但是可以通过一个巧妙的方法来解决。首先,记录下所有不会被拆除的边,然后按照逆序处理这些指令。遇到拆边操作时,将该边重新加入并查集中即可。 在实现并查集时,虽然它是一种树形结构,但只需要使用数组就可以实现。可以通过将每个元素的父节点记录在数组中,来表示元素之间的关系。通过路径压缩和按秩合并等优化策略,可以提高并查集的效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [「kuangbin专题五并查集专题题解](https://blog.csdn.net/weixin_51216553/article/details/121643742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [并查集(详细解释+完整C语言代码)](https://blog.csdn.net/weixin_54186646/article/details/124477838)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值