• [kuangbin] M


    题目链接: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;
    }
  • 相关阅读:
    RCP二级菜单的实现
    volatile 的作用
    C6678 核间通信(IPC)
    大小端模式详解
    SPI 极性与相位对采样的影响
    DSP c6678的启动方式
    DSP CCS初学
    volatile的作用
    JTree/DefaultMutableTreeNode 树形结构
    java.awt.Graphics2D绘制流程图基本元素
  • 原文地址:https://www.cnblogs.com/Fy1999/p/8656526.html
Copyright © 2020-2023  润新知