HDU-2612,Find a way(BFS)

简介: HDU-2612,Find a way(BFS)

Problem Description:


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<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define maxn 201
#define INF 0x3f3f3f
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int dist1[maxn][maxn],dist2[maxn][maxn];
int vis[maxn][maxn],m,n;
char map[maxn][maxn];
struct node
{
  int x,y,step;
};
void bfs(node start,int dist[maxn][maxn])
{
  memset(vis,0,sizeof(vis));
  queue<node> Q;
  node p,q;
  Q.push(start);
  vis[start.x][start.y]=1;
  dist[start.x][start.y]=0;
  while(!Q.empty())
  {
    p=Q.front();
    Q.pop();
    for(int i=0;i<4;i++)
    {
      q.x=p.x+dir[i][0];
      q.y=p.y+dir[i][1];
      if(!vis[q.x][q.y]&&q.x>=0&&q.x<n&&q.y>=0&&q.y<m&&map[q.x][q.y]!='#')
      {
        vis[q.x][q.y]=1;
        dist[q.x][q.y]=dist[p.x][p.y]+1;
        Q.push(q);
      }
    }
  }
}
int main()
{
  while(~scanf("%d %d",&n,&m))
  {
    node s1,s2;
    getchar();
    for(int i=0;i<n;i++,getchar())
    {
      for(int j=0;j<m;j++)
      {
        scanf("%c",&map[i][j]);
        if(map[i][j]=='Y')
        {
          s1.x=i;
          s1.y=j;
          s1.step=0;
        }
        if(map[i][j]=='M')
        {
          s2.x=i;
          s2.y=j;
          s2.step=0;
        }
      }
    }
    memset(dist1,0,sizeof(dist1));
    memset(dist2,0,sizeof(dist2));
    bfs(s1,dist1);
    bfs(s2,dist2);
    int minn=INF;
    for(int i=0;i<n;i++)
    {
      for(int j=0;j<m;j++)
      {
        if(map[i][j]=='@'&&dist1[i][j]!=0&&dist2[i][j]!=0)
          minn=min(minn,dist1[i][j]+dist2[i][j]);
      }
    }
    printf("%d\n",minn*11);
  }
  return 0;
}


相关文章
|
6月前
|
机器学习/深度学习 安全 Java
hdu-1596-find the safest road(dijkstra)
hdu-1596-find the safest road(dijkstra)
40 0
HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)
HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)
108 0
|
并行计算
Find a way(两个BFS)
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally.
1107 0
【HDU 4786 Fibonacci Tree】最小生成树
一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1。 给出n、m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中的一个。
1069 0