简单搜索-HDU2612(两个BFS)

HDU2612 Find a way

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

题意

@代表KFC
Y和M是两个人要约KFC
‘.’是可以走的路
‘#’是不可以走的路
问两个人总共走的路程的最小值

思路

(碎碎念time:刚开始是用了记下每个KFC的位置,然后对每个KFC用bfs求出两人到这个KFC的距离和然后取最小的一个和,其实是怪麻烦的就初始化什么的,虽然解决了这个令人头秃的问题,然而这样子超时了啊,)
两个人分开BFS
并存起每个人到不同KFC的最小步数
然后最后加起来比较输出最小的一个

介个是TLE代码

#include<cstdio>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

#define PI         acos(-1)
#define inf        0x3f3f3f3f
#define EPS        1e-6
#define mem(a, b)  memset(a, b, sizeof(a))
#define ll long long
#define mian main
char imap[210][210];
bool vis[210][210] = {0};
struct node
{
    int x;
    int y;
    int s;
};
queue<node> q1;
queue<node> q2;
int n, m;
int dir[4][2] = {1,0, -1,0, 0,1, 0,-1};
int bfs1(node b)
{
    node pre;
    while (!q1.empty())
    {
         pre = q1.front();
         q1.pop();
         if (pre.x == b.x && pre.y == b.y)
            return pre.s;
         node re;
         for (int i = 0; i < 4; i++)
         {
             re.x = pre.x + dir[i][0];
             re.y = pre.y + dir[i][1];
             re.s = pre.s + 1;
             if(!vis[re.x][re.y] && imap[re.x][re.y] != '#' && re.x >= 0 && re.x < n &&re.y >= 0 && re.y < m)
             {
                 //cout<<re.x<<' '<<re.y<<' '<<re.s<<endl;
                 vis[re.x][re.y] = 1;
                 q1.push(re);
             }
         }
         pre.s = 0;
    }


}
int bfs2(node b)
{
    node pre;
    while (!q2.empty())
    {
         pre = q2.front();
         q2.pop();
         if (pre.x == b.x && pre.y == b.y)
            return pre.s;
         node re;
         for (int i = 0; i < 4; i++)
         {
             re.x = pre.x + dir[i][0];
             re.y = pre.y + dir[i][1];
             re.s = pre.s + 1;
             if(!vis[re.x][re.y] && imap[re.x][re.y] != '#' && re.x >= 0 && re.x < n &&re.y >= 0 && re.y < m)
             {
                 //cout<<re.x<<' '<<re.y<<' '<<re.s<<endl;
                 vis[re.x][re.y] = 1;
                 q2.push(re);
             }
         }
         pre.s = 0;
    }


}
int main()
{

    node yy, mm;
    node kfc[20000];
    int ans1[20000];
    int ans2[20000];
    int ans[20000];
    while (cin>>n>>m)
    {
        int no = 0;
        for (int i = 0; i < n; i ++)
        {
            for (int j = 0; j < m; j++)
            {
                char c;
                cin >> c;
                imap[i][j] = c;
                if (c == 'Y')
                {
                    yy.x = i;
                    yy.y = j;
                    yy.s = 0;
                    //q1.push(yy);
                }
                if (c == 'M')
                {
                    mm.x = i;
                    mm.y = j;
                    mm.s = 0;
                    //q2.push(mm);
                }
                if (c == '@')
                {
                    kfc[no].x = i;
                    kfc[no].y = j;
                    kfc[no].s = 0;
                    //cout<<i<<' '<<j<<endl;
                    no++;
                }

            }

        }
        //cout << "no" << no << endl;
        int imin = inf;
        for (int i = 0; i < no; i++)
        {
            while (!q1.empty())
                q1.pop();
            while (!q2.empty())
                q2.pop();
            q1.push(yy);
            q2.push(mm);
           // kfc[i].s = 0;
            vis[yy.x][yy.y] = 1;
            ans1[i] = bfs1(kfc[i]);
            mem(vis, 0);
            //kfc[i].s = 0;
            vis[mm.x][mm.y] = 1;
            ans2[i] = bfs2(kfc[i]);
            mem(vis, 0);
            ans[i] = ans1[i] + ans2[i];
            if(ans[i] < imin)
                imin = ans[i];
            //cout << ans[i] << endl;
        }
        cout << imin*11 << endl;
    }
}

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
char mp[210][210];
int a[210][210],b[210][210];
bool vis[210][210];
int n,m;
int ans;
struct node
{
    int x,y;
    int step;
} re,s,ns;
queue<node> q;
int dir[4][2]= {{-1,0},{0,-1},{1,0},{0,1}};
bool jud(int x,int y)
{
    if(x>=0 && y>=0 && x<n && y<m && !vis[x][y] && mp[x][y]!='#')
        return 1;
    return 0;
}
void bfs1(int x,int y)
{
    re.x=x;
    re.y=y;
    re.step=0;
    vis[x][y]=1;
    q.push(re);
    while(!q.empty())
    {
        s=q.front();
        q.pop();
        if(mp[s.x][s.y]=='@')
        {
            a[s.x][s.y]=s.step;
        }
        for(int i = 0; i < 4; i ++)
        {
            int ix=s.x+dir[i][0];
            int iy=s.y+dir[i][1];
            if(jud(ix,iy))
            {
                vis[ix][iy]=1;
                //已经游历就标记为false
                ns.x=ix;
                ns.y=iy;
                ns.step=s.step+1;
                //cout<<'b'<<ix<<' '<<iy<<' '<<ns.step<<endl;
                q.push(ns);                //将下一位置元素压入队列中进行判断
            }
        }
    }
}

void bfs2(int x, int y)
{
    re.x=x;
    re.y=y;
    re.step=0;
    vis[x][y]=1;
    q.push(re);
    while(!q.empty())
    {
        s=q.front();
        q.pop();
        if(mp[s.x][s.y]=='@')
        {
            b[s.x][s.y]=s.step;
        }
        for(int i=0; i<4; i++)
        {
            int ix=s.x+dir[i][0];
            int iy=s.y+dir[i][1];
            if(jud(ix,iy))
            {
                vis[ix][iy]=1;
                        //已经游历就标记为false
                ns.x=ix;
                ns.y=iy;
                ns.step=s.step+1;
                //cout<<'a'<<ix<<' '<<iy<<' '<<ns.step<<endl;
                q.push(ns);                //将下一位置元素压入队列中进行判断
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        ans=inf;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                a[i][j]=inf;
                b[i][j]=inf;
            }
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                cin>>mp[i][j];
            }
        }
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
            {
                if(mp[i][j]=='Y')
                {
                    memset(vis,0,sizeof(vis));
                    bfs1(i,j);
                }
                if(mp[i][j]=='M')
                {
                    memset(vis,0,sizeof(vis));  //记得再次初始化标记数组
                    bfs2(i,j);
                }
            }
        for(int i =0; i<n; i++)
            for(int j=0; j<m; j++)
                if(mp[i][j]=='@')
                {
                    //cout<<a[i][j]<<' '<<b[i][j]<<endl;
                    if(ans > (a[i][j] + b[i][j]))
                        ans = a[i][j] + b[i][j];
                }
        printf("%d\n",ans*11);
    }
    return 0;
}

我不是第一次写代码了;
我错了;
蟹蟹师哥哥给debug~~~

猜你喜欢

转载自blog.csdn.net/wuswi0412/article/details/81187853