Find a way HDU - 2612(两起点bfs)

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

题意:

两个起点,问都到达@的总共最少时间*11

分析:

两遍bfs即可

code:

#include 
using namespace std;
const int INF = 0x3f3f3f3f;
int n,m;
mapint,int>,pair<int,int> >kfc;
char mp[220][220];
bool vis[220][220];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node{
    int x,y;
    int dis;
};
void bfs(pair<int,int> s,int id){
    memset(vis,0,sizeof(vis));
    queue q;
    node now,Next;
    now.x = s.first;
    now.y = s.second;
    now.dis = 0;
    q.push(now);
    vis[s.first][s.second] = 1;
    while(!q.empty()){
        now = q.front();
        q.pop();
        if(mp[now.x][now.y] == '@'){
            int d = now.dis;
            pair<int,int> tmp = make_pair(now.x,now.y);
            pair<int,int> pos = tmp;
            tmp = kfc[pos];
            if(id == 1){
                int t1 = min(tmp.first,d);
                int t2 = tmp.second;
                kfc[pos] = make_pair(t1,t2);
            }
            if(id == 2){
                int t1 = tmp.first;
                int t2 = min(tmp.second,d);
                kfc[pos] = make_pair(t1,t2);
            }
         }
        for(int i = 0; i < 4; i++){
            int xx = now.x + dir[i][0];
            int yy = now.y + dir[i][1];
            if(xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy] && mp[xx][yy] != '#'){
                Next.x = xx;
                Next.y = yy;
                vis[xx][yy] = 1;
                Next.dis = now.dis + 1;
                q.push(Next);
            }
        }
    }
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        pair<int,int> yi,me;
        kfc.clear();//注意初始化
        for(int i = 0; i < n; i++){
            scanf("%s",mp[i]);
            for(int j = 0; j < m; j++){
                if(mp[i][j] == '@')
                    kfc[make_pair(i,j)] = make_pair(INF,INF);
                else if(mp[i][j] == 'Y')
                    yi = make_pair(i,j);
                else if(mp[i][j] == 'M')
                    me = make_pair(i,j);
            }
        }

        bfs(yi,1);
        bfs(me,2);
        mapint,int>,pair<int,int> >::iterator it;
        int ans = INF;
        for(it = kfc.begin(); it != kfc.end(); it++){
            pair<int,int> tmp = it->second;
            ans = min(ans,tmp.first+tmp.second);
        }
        printf("%d\n",ans*11);
    }
    return 0;
}

你可能感兴趣的:(BFS)