[kuangbin] M - Find a way(简单广搜)

题目链接:https://vjudge.net/contest/215603#problem/M

 

其中三维数组dis将两个广搜合并到了一起

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF=220;
const int M=0x1f1f1f1f;
int n,m,z;
char cnt[INF][INF];
int dir[][2]= {{1,0},{0,-1},{-1,0},{0,1}};
int dis[INF][INF][2];
int vis[INF][INF];

struct node
{
    int x,y;
    int step;
    node(int x,int y,int z):x(x),y(y),step(z){}
    node() {}
};



void BFS(int x,int y)
{
    memset(vis,0,sizeof(vis));
    vis[x][y]=1;
    queue<node>q;
    q.push(node(x,y,0));
    while(!q.empty())
    {
        node top=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int newx=top.x+dir[i][0];
            int newy=top.y+dir[i][1];
            if(!vis[newx][newy]&& newx>=1&&newx<=n&&newy>=1&&newy<=m&&cnt[newx][newy]!='#')
            {
                vis[newx][newy]=1;
                dis[newx][newy][z]=top.step+1;
                q.push(node(newx,newy,top.step+1));
            }
        }

    }
}

int main()
{
    while(cin>>n>>m)
    {
        memset(dis,M,sizeof(dis));
        for(int i=1; i<=n; i++)
            scanf("%s",cnt[i]+1);
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                if(cnt[i][j]=='Y')
                {
                    z=1;
                    BFS(i,j);
                }
                if(cnt[i][j]=='M')
                {
                    z=0;
                    BFS(i,j);
                }

            }

        int ans=M;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                if(cnt[i][j]=='@')
                   {
                        ans=min(ans,dis[i][j][0]+dis[i][j][1]);

                   }
            }
        }
        cout<<ans*11<<endl;

    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Fy1999/p/8656526.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值