Find a way (HDU 2612)

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

思路
1.一开始使用从‘@‘跑到Y,M的方法,遍历每一个’@‘,即每一个’@‘都要跑一边bfs,然后就超时了
2.然后换成Y,M跑到’@‘,并分别记录下Y,M跑到每一个’@‘的时间,遍历’@'求出最小的和即可,这样只跑2遍

#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;

int n,m;
char s[205][205];
int ans[3][205][205];
int X[4]={1,-1,0,0};
int Y[4]={0,0,1,-1};
bool visit[205][205];
struct node
{
    int x,y,step;
    node()
    {
        step=0;
    }
    node(int a,int b,int d)
    {
        x=a,y=b,step=d;
    }
}S,e[5],temp;


bool check(int x,int y)
{
    if(s[x][y]=='#'||x<1||x>n) return false;
    if(y>m||y<1||visit[x][y]) return false;
    return true;
}

void solve(int p)
{
    visit[e[p].x][e[p].y]=true;
    ans[p][e[p].x][e[p].y]=0;
    queue<node> que;
    que.push(e[p]);
    while(!que.empty())
    {
        temp=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int nx=temp.x+X[i];
            int ny=temp.y+Y[i];
            if(check(nx,ny))
            {
                visit[nx][ny]=true;
                que.push({nx,ny,temp.step+1});
                if(s[nx][ny]=='@') ans[p][nx][ny]=temp.step+1;
            }
        }
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    cout.tie(0);
    while(cin>>n>>m)
    {
        memset(s,0,sizeof(s));
        memset(visit,0,sizeof(visit));
        memset(ans,0,sizeof(ans));
        int cnt=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>s[i][j];
                if(s[i][j]=='Y') e[1].x=i,e[1].y=j;
                if(s[i][j]=='M') e[2].x=i,e[2].y=j;
            }
        }
        int Ans=1000000;
        solve(1);
        memset(visit,0,sizeof(visit));
        solve(2);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(s[i][j]=='@'&&ans[1][i][j]&&ans[2][i][j])
                    Ans=min(Ans,ans[1][i][j]+ans[2][i][j]);
            }
        }
        cout<<Ans*11<<'\n';
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值