Find a way HDU - 2612 (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

题意:Y和M都去同一个@,问最短距离(x*11).

思路:双向bfs。一开始我只简单的想bfs,对每个@求最短路径,但是TLM,因为每次都重复计算了太多。后来发现了一种类似dfs记忆化搜索的办法,把从y出发能到达的点的最短长度存在ans_y中,从m出发的存在ans_m中,最后计算求最短长度即可。 

PS:之前定义的一个变量为next,发现老是编译错误,换个名字就可以了。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#define maxn 205
using namespace std;

char maze[maxn][maxn];
int ans_y[maxn][maxn];
int ans_m[maxn][maxn];
int d[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
int n,m,yst_x,yst_y,mst_x,mst_y;

struct Node{
    int x,y,step;
};
Node nows;
Node nexts;
void bfs(int sx,int sy,int flag)
{
    queue<Node> q;
    nows.x = sx;
    nows.y = sy;
    nows.step = 0;
    q.push(nows);
    while(!q.empty()){
        nows = q.front();
        q.pop();
        for(int i=0;i<4;++i){
            nexts.x = nows.x + d[i][0];
            nexts.y = nows.y + d[i][1];
            nexts.step = nows.step + 1;
            if(nexts.x<1||nexts.x>n||nexts.y<1||nexts.y>m) continue;
            if(maze[nexts.x][nexts.y]=='#') continue;
            if(flag==1){
                //当Y到此点的距离已经计算过,并且Y到此点的距离已经是当前最小,此时不更新
                if(ans_y[nexts.x][nexts.y]!=0&&ans_y[nexts.x][nexts.y]<=nexts.step) continue;
                ans_y[nexts.x][nexts.y] = nexts.step; //当发现有距离更小的时,更新
            }
            else{
                //同上
                if(ans_m[nexts.x][nexts.y]!=0&&ans_m[nexts.x][nexts.y]<=nexts.step) continue;
                ans_m[nexts.x][nexts.y] = nexts.step;
            }
            q.push(nexts);
        }
    }
    return ;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF){
        getchar();
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j){
                scanf("%c",&maze[i][j]);
                if(maze[i][j]=='Y'){
                    yst_x = i;yst_y = j;
                }
                if(maze[i][j]=='M'){
                    mst_x = i;mst_y = j;
                }
            }
            getchar();
        }

        memset(ans_y,0,sizeof(ans_y));
        bfs(yst_x,yst_y,1);
        memset(ans_m,0,sizeof(ans_m));
        bfs(mst_x,mst_y,2);

        int ans = 999999;
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j){
                if(maze[i][j] == '@'){
                    if(ans_y[i][j]!=0&&ans_m[i][j]!=0){  //代表Y和M都能到达此点
                        ans = min(ans_y[i][j]+ans_m[i][j],ans);
                    }
                }
            }
        }
        printf("%d\n",ans*11);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值