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;
}


相关文章
|
7月前
|
机器学习/深度学习 安全 Java
hdu-1596-find the safest road(dijkstra)
hdu-1596-find the safest road(dijkstra)
50 0
华为机试HJ44:Sudoku(数独问题,深度优先遍历DFS解法)
华为机试HJ44:Sudoku(数独问题,深度优先遍历DFS解法)
152 0
|
存储 容器
华为机试HJ41:称砝码(深度优先遍历dfs-Depth First Search)
华为机试HJ41:称砝码(深度优先遍历dfs-Depth First Search)
163 0
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
[路飞]_leetcode-589-N 叉树的前序遍历
[路飞]_leetcode-589-N 叉树的前序遍历