hdu 2612 Find a way

本文介绍了一种通过广度优先搜索算法找到两个不同起点的人在地图上能够最快相遇的KFC位置的方法。该算法考虑了宁波城市地图上的障碍物及通行路径,并确保两人能以最短时间到达同一KFC。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

找一个kfc的位置,让两个人花费的总时间最少(PS:他们不处于同一时间下吗,为什么要找时间和,题目说啥就是啥吧)

从两个人的位置开始做两次广搜,记录下到kfc需要的步数,最后把步数相加,找一下在可达的kfc里面最小的时间花费就行了。忘记把动态数组初始化,看着大佬们一遍就过,而我一直WA到比赛结束。。。

#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int M = 205;
const int INF = 99999999;
struct Node
{
    int x, y, s;
};
char mapp[M][M];
int mv[4][2] = {{0,-1},{0,1},{1,0},{-1,0}};
vector<Node> kfc;
int n, m, yx, yy, mx, my;
int tim[M][M];
bool bo[M][M];
void bfs(int x, int y)
{
    memset(bo, false, sizeof(bo));
    Node tmp, nex;
    queue<Node> q;
    tmp.x = x;
    tmp.y = y;
    tmp.s = 0;
    tim[x][y] = 0;
    bo[x][y] = true;
    q.push(tmp);
    while(!q.empty())
    {
        tmp = q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            nex.x = tmp.x + mv[i][0];
            nex.y = tmp.y + mv[i][1];
            nex.s = tmp.s + 1;
            if(nex.x<1||nex.x>n||nex.y<1||nex.y>m)
                continue;
            if(mapp[nex.x][nex.y]!='#'&&!bo[nex.x][nex.y])
            {
                bo[nex.x][nex.y] = true;
                tim[nex.x][nex.y] += nex.s;
                q.push(nex);
            }
        }
    }
}
int main()
{
    Node tmm;
    int ans;
    while(~scanf("%d%d", &n, &m))
    {
        kfc.clear();//这句老是忘记,坑的是不加这一句我试的每组样例都还能过!!??
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                scanf(" %c", &mapp[i][j]);
                tim[i][j] = 0;
                if(mapp[i][j]=='Y')
                {
                    yx = i;
                    yy = j;
                }
                else if(mapp[i][j]=='M')
                {
                    mx = i;
                    my = j;
                }
                else if(mapp[i][j]=='@')
                {
                    tmm.x = i;
                    tmm.y = j;
                    kfc.push_back(tmm);
                }
            }
        bfs(yx, yy);
        bfs(mx, my);
        ans = INF;
        vector<Node>::iterator it=kfc.begin();
        for(;it!=kfc.end();it++)
            if(tim[it->x][it->y]!=0)
                ans = min(ans, tim[it->x][it->y]);
        printf("%d\n", 11*ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值