14.找路

原题链接:https://www.acwing.com/problem/content/description/4230/

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

#define x first
#define y second
typedef pair<int,int> PII;
const int N=210;
int n,m;
char g[N][N];
int dist1[N][N],dist2[N][N];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};

void bfs(PII a,int dist[N][N])
{
    queue<PII> q;
    q.push(a);
    memset(dist,0x3f,4*N*N);
    dist[a.x][a.y]=0;
    while(q.size())
    {
        auto t=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int x=dx[i]+t.x,y=dy[i]+t.y;
            if(x<0||x>=n||y<0||y>=m) continue;
            if(g[x][y]=='#') continue;
            if(dist[x][y]==0x3f3f3f3f){
                dist[x][y]=dist[t.x][t.y]+1;
                q.push({x,y});
            }
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++) scanf("%s",&g[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(g[i][j]=='Y') bfs({i,j},dist1);
                if(g[i][j]=='M') bfs({i,j},dist2);
            }
        }
        int ans=1e9;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(g[i][j]=='@'){
                    ans=min(ans,dist1[i][j]+dist2[i][j]);
                }
            }
        cout<<ans*11<<endl;
    }
    return 0;
}

posted on   skaman  阅读(25)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示