hdoj 2612 Find a way (bfs,双向搜索)

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10380    Accepted Submission(s): 3392


Problem Description
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
 


双向搜索,开始写两个搜索,分别是J到每个@的时间和M到每个@的时间,用一维数组存的,结果超时,改了还超,

看了雨神的,发现他的方法好好,代码还短,从中学到不少。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#define INF 0x3f3f3f
using namespace std;

int min(int x,int y)//最小最大函数别再马虎了,又因为这个麻烦师傅了^^^
{
    if(x<y)return x;
    return y;
}
int n,m;
int sx,sy,ex,ey;
int time1[210][210],time2[210][210];
int vis[210][210];
char map[210][210];
int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
struct node // bfs使用结构体
{
    int x,y,step;
};
node a,b;
void bfs(int x,int y,int p)//void 函数是返回值为空,括号内的形参是否有只是看自己的思路写的
{                          //这里是双向bfs ,记住这个样例
    queue<node> q;
    a.x=x;
    a.y=y;
    a.step=0;
    vis[x][y]=1;
    q.push(a);
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        if(map[a.x][a.y]=='@')//寻找每一个店,并判断是向哪个方向走
        {
            if(p==1)
                time1[a.x][a.y]=a.step;//用时间数组记录每个点的步数
            else
                time2[a.x][a.y]=a.step;
        }
        for(int i=0;i<4;i++)//四个方向搜索
        {
            b.x=a.x+dir[i][0];
            b.y=a.y+dir[i][1];
            if(map[b.x][b.y]!='#'&&vis[b.x][b.y]==0&&b.x>=0&&b.x<n&&b.y>=0&&b.y<m)
            {
                vis[b.x][b.y]=1;
                b.step=a.step+1;
                q.push(b);
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(int j=0;j<m;j++)
            {
                if(map[i][j]=='Y')
                {
                    sx=i;sy=j;
                }
                if(map[i][j]=='M')
                {
                    ex=i;ey=j;
                }
            }
        }
        //双向搜索,先初始化,再搜索
        memset(vis,0,sizeof(vis));
        memset(time1,INF,sizeof(time1));
        bfs(sx,sy,1);
        memset(vis,0,sizeof(vis));
        memset(time2,INF,sizeof(time2));
        bfs(ex,ey,2);
        int minn=INF;//最小值最大化。
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(time1[i][j]!=INF&&time2[i][j]!=INF)
                   minn=min(minn,time1[i][j]+time2[i][j]);
            }
        }
        printf("%d\n",minn*11);
    }
    return 0;
}



(自己敲得时候还是状况百出……)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值