HDU2612 Find a way (跑两遍BFS)

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.

InputThe 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
OutputFor 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
注意几个地方:1.必须两个人都要访问到那个肯德基才能用它更新答案。2.Y,M,肯德基都可以经过。3.搜到肯德基以后万万不能直接continue,因为肯德基能经过。万一有
..Y.
##@#
##@#
..M.
这样的数据就会WA!!没注意到这里卡了一下午QnQ
复制代码
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int x;
    int y;
    int time;
};
int n,m;
char mmap[405][405];
int ans[405][405][2];
bool vis[405][405];
const int INF=0x3f3f3f3f; 

int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void bfs(int p,int sx,int sy)//p为0:Y p为1:M  sx、sy为起点坐标 
{
    queue<node>q;
    node start;
    start.x=sx,start.y=sy,start.time=0;
    q.push(start);
    vis[sx][sy]=1;
    while(q.size())
    {
        node pre;
        pre=q.front();
        q.pop();
        if(mmap[pre.x][pre.y]=='@')
        {
            ans[pre.x][pre.y][p]=pre.time*11;// p为0:Y花的时间 p为1:M花的时间 
        //    continue;千万不能写continue!!!! 
        }
        int i;
        for(i=0;i<=3;i++)
        {
            int nx=pre.x+dir[i][0];
            int ny=pre.y+dir[i][1];
            if(!vis[nx][ny]&&nx>=0&&nx<n&&ny>=0&&ny<m&&mmap[nx][ny]!='#')
            {
                node nxt;
                nxt.x=nx;
                nxt.y=ny;
                nxt.time=pre.time+1;
                q.push(nxt);
                vis[nx][ny]=1;
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(ans,0,sizeof(ans));
        memset(mmap,0,sizeof(mmap));
        memset(vis,0,sizeof(vis));
        int i,j;
        for(i=0;i<n;i++)
        {
            scanf("%s",mmap[i]);
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(mmap[i][j]=='Y')
                {
                    memset(vis,0,sizeof(vis));
                    bfs(0,i,j);
                }
                else if(mmap[i][j]=='M')
                { 
                    memset(vis,0,sizeof(vis));
                    bfs(1,i,j);
                }
            }
        }    
        int out=INF;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(ans[i][j][0]!=0&&ans[i][j][1]!=0)out=min(out,ans[i][j][0]+ans[i][j][1]);//必须要两个人都能到kfc 
            }
        }    
        cout<<out<<endl;
    }    
}
复制代码

 


posted @   脂环  阅读(148)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示
主题色彩