H - Find a way(两次bfs)

H - Find a way

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

题意:
Y和M到达同一个’@’点的最短距离(注意算完步数之后,要*11)
bfs 两次,要注意在第二次开始的时候,要将标记数组清零~

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 267
char ma[MAXN][MAXN];//存图
int n, m;//图的大小
int v[MAXN][MAXN];//标记数组
struct node
{
    int x, y, step;//x坐标,y坐标,及到达此点的步数
}t, p, q, que[121212];
int ans[MAXN][MAXN];//记录到达@点的步数
int vectors[][2] = {1, 0, -1, 0, 0, 1, 0, -1};//上下左右四个方向
void bfs(int x, int y)
{
    v[x][y] = 1;//标记
    int front = 0, rear = 0;
    p.x = x;
    p.y = y;
    p.step = 0;
    que[rear++] = p;//入队
    while(front<rear)
    {
        q = que[front++];
        for(int i=0;i<4;i++)//四个方向遍历
         {
            p.x = q.x + vectors[i][0];
            p.y = q.y + vectors[i][1];
            if(p.x<0||p.x>=n||p.y<0||p.y>=m||v[p.x][p.y]||ma[p.x][p.y]=='#')
                 continue;//不符条件
            if(ma[p.x][p.y]=='@')//如果是终点
            {
                ans[p.x][p.y] += q.step + 1;//记录步数
            }
            v[p.x][p.y] = 1;//标记
            p.step = q.step+1;//更新步数
            que[rear++] = p;//入队
            }
    }
    return ;
}
int main()
{

    while(~scanf("%d %d", &n, &m))
    {
    memset(ans, 0, sizeof(ans));//清零
    for(int i=0;i<n;i++)
        scanf("%s", ma[i]);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(ma[i][j]=='Y')
            {
                t.x = i;//记录横坐标
                t.y = j;//记录竖坐标
                t.step = 0;
            }
        }
    }
    memset(v, 0, sizeof(v));
    bfs(t.x, t.y);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(ma[i][j]=='M')
            {
                t.x = i;
                t.y = j;
                t.step = 0;
            }
        }
    }
    memset(v, 0, sizeof(v));//记得清零
    bfs(t.x, t.y);
    int min = 989898;//定义一个大一点的数
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
              if(ans[i][j]<min&&ans[i][j])
                    min = ans[i][j];
        }
    }
    printf("%d\n", min*11);//别忘了*11
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值