题解:

M和Y约在KFC见面,问两个人去到同一家KFC所用的最短时间,直接求出M和Y到所有点的最短时间,然后枚举到KFC的最小值即可

代码

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define MAX 100000
#define LL long long
int cas=1,T;
char mapp[201][201];
int vis[201][201];
int county[201][201];
int countm[201][201];
struct Node
{
    int x;
    int y;
    int countt;
};
queue<Node> que;
int n,m;
bool check(int x,int y)
{
   if (x<0 || y<0 || x>n || y>m)
       return true;
   if (vis[x][y] || mapp[x][y]=='#')
       return true;
   return false;
}
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int main()
{
    while (scanf("%d%d",&n,&m)!=EOF)            //忘记加 !=EOF  TLE两次!
    {
       Node ys,ms;
       for (int i = 0;i<n;i++)
           scanf("%s",&mapp[i]);
       for (int i = 0;i<n;i++)
           for (int j = 0;j<m;j++)
           {
              if (mapp[i][j]=='Y')
              {
                  ys.x=i;
                  ys.y=j;
                  ys.countt=0;
              }
              if (mapp[i][j]=='M')
              {
                  ms.x=i;
                  ms.y=j;
                  ms.countt=0;
              }
           }
        vis[ys.x][ys.y]=1;
        que.push(ys);
        while (!que.empty())
        {
            Node t=que.front();
            que.pop();
            for (int i = 0;i<4;i++)
            {
                Node temp;
                temp.x = t.x+dir[i][0];
                temp.y = t.y+dir[i][1];
                temp.countt=t.countt+1;
                if (check(temp.x,temp.y))
                    continue;

                vis[temp.x][temp.y]=1;
                county[temp.x][temp.y]=temp.countt;
                que.push(temp);

            }
        }

        while (!que.empty())
            que.pop();
        memset(vis,0,sizeof(vis));
        vis[ms.x][ms.y]=1;
        que.push(ms);
        while (!que.empty())
        {
           Node t = que.front();
           que.pop();
           for (int i = 0;i<4;i++)
           {
               Node temp;
               temp.x = t.x+dir[i][0];
               temp.y = t.y+dir[i][1];
               temp.countt=t.countt+1;
               if (check(temp.x,temp.y))
                   continue;
               vis[temp.x][temp.y]=1;
               countm[temp.x][temp.y]=temp.countt;
               que.push(temp);
           }
        }
        int mins = 99999999;
       for (int i = 0;i<n;i++)
           for (int j = 0;j<m;j++)
           {
               if (mapp[i][j]=='@')
                 if (county[i][j]+countm[i][j] < mins)
                     mins=county[i][j] + countm[i][j];
           }
       printf("%d\n",mins*11);
       memset(mapp,0,sizeof(mapp));
       memset(countm,0,sizeof(countm));
       memset(county,0,sizeof(county));
       memset(vis,0,sizeof(vis));
       while (!que.empty())
           que.pop();
    }

    return 0;
}