BFS:HDU2612-Find a way(双向BFS)

Find a way

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

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




解题心得:

1、关于双向bfs的判定,一般是有两个移动的点,没有目标或者一个目标,当有三个或者更多的移动的点的时候一般要考虑是否可以用一些技巧消去一些。


2、双向bfs在找最小的路径之和的时候并不是找到了之后马上跳出,因为并不是第一个相遇的点一定是最短的点,所以在不超时,数据量比较小的时候尽量跑完,不然很可能在某个角落差生错误,找都找不到,在跑双向bfs 的时候标记一定要弄好,不然很目标是一个bfs跑了两次,这就很恼火了。



#include<bits/stdc++.h>
using namespace std;
const int maxn = 210;
char maps[maxn][maxn];
int y_x,y_y,m_x,m_y;
int dir[4][2] ={0,1,0,-1,1,0,-1,0};
int n,m,Min,num_2;
bool use[2][maxn][maxn];//两个bfs,标记也要开两层
struct Vis
{
    int num;//记录这个@点被找到了几次
    bool is;
    int sum;//用来记录两个bfs在@点相遇的时间的和
} vis[maxn][maxn];
struct node
{
    int x,y;
};
queue<node>q[2],qt;//两个bfs的队列和层使用的队列

bool check(int x,int y)//检查一下可不可以走到那里
{
    if(x<0 || y<0 || x>=n || y>=m)
        return true;
    if(maps[x][y] == '#')
        return true;
    return false;
}
void pre_maps()
{
    //初始化很重要
    Min = 0x7f7f7f7f;
    memset(use,0,sizeof(use));
    num_2 = 0;
    while(!q[0].empty())
        q[0].pop();
    while(!q[1].empty())
        q[1].pop();
    while(!qt.empty())
        qt.pop();

    for(int i=0; i<n; i++)
        scanf("%s",maps[i]);
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
        {
            vis[i][j].is = false;
            vis[i][j].num = 0;
            vis[i][j].sum = 0;
            if(maps[i][j] == 'Y')
            {
                y_x = i;
                y_y = j;
                use[0][y_x][y_y] = true;
            }
            if(maps[i][j] == 'M')
            {
                m_x = i;
                m_y = j;
                use[1][m_x][m_y] = true;
            }
            if(maps[i][j] == '@')//将@记录一下,在后面直接判断就行了
            {
                vis[i][j].is = true;
            }
        }
}

bool bfs(int num,int ans)
{
    node now,Next;
    qt = q[num];
    while(!qt.empty())
    {
        now = qt.front();
        qt.pop();
        q[num].pop();
        for(int i=0; i<4; i++)
        {
            Next.x = now.x + dir[i][0];
            Next.y = now.y + dir[i][1];
            if(check(Next.x,Next.y)) continue;
            if(vis[Next.x][Next.y].is && !use[num][Next.x][Next.y])
            {
                if(vis[Next.x][Next.y].num == 1)//被两个bfs找到,并且是两个不同的bfs,一定要做好标记啊,不然很恼火的,是两个不同的bfs
                {
                    vis[Next.x][Next.y].sum += ans;//两次的时间和
                    Min = min(Min,vis[Next.x][Next.y].sum);//记录两个不同的bfs到达@的时间和的最小的那个
                }
                else if(vis[Next.x][Next.y].num == 0)//被一个bfs被找到
                {
                    vis[Next.x][Next.y].sum += ans;
                    vis[Next.x][Next.y].num ++;
                }
            }
            if(!use[num][Next.x][Next.y])
            {
                use[num][Next.x][Next.y] = true;
                q[num].push(Next);
            }
        }
    }
}
int get_ans()
{
    int ans = 0;
    node now;
    now.x = y_x;
    now.y = y_y;
    q[0].push(now);
    now.x = m_x;
    now.y = m_y;
    q[1].push(now);

    bool flag1 = false;
    bool flag2 = false;
    while(!q[0].empty() || !q[1].empty())//两个bfs一层一层的跑
    {
        ans += 11;//每走一步花11分钟
        bfs(0,ans);
        bfs(1,ans);
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        pre_maps();
        get_ans();
        printf("%d\n",Min);
    }
    return 0;
}




posted @ 2017-07-06 21:01  GoldenFingers  阅读(133)  评论(0编辑  收藏  举报