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


题意: Y和M要去同一个肯德基会面,但是这个地方有很多肯德基,问到哪个肯德基会面所用时间最短,@标识肯德基。

分析: 坑点在Y能到的肯德基,M不一定能到,所以只有当两人都能到时才算数。其它的话,我是分别bfs,记录一个人到每个肯德基的时间,然后再bfs另一个人。






#include<iostream>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
using namespace std;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
typedef long long ll;
bool ans[205][205];
int res[205][205];
int people[205][205];
int n,m;
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
struct P{
    int x,y,step;
};
void bfs(int x,int y,bool vis[][205],int cnt){
    queue<P> q;
    P temp;
    temp.x = x;
    temp.y = y;
    temp.step = 0;
    q.push(temp);
    while(!q.empty()){
        temp = q.front();
        q.pop();
        if(ans[temp.y][temp.x] == 1){
            res[temp.y][temp.x] += temp.step;
            vis[temp.y][temp.x] = 0;
            people[temp.y][temp.x]++;
            cnt--;
            if(cnt == 0) return;
        }
        for(int i=0;i<4;i++){
            P dt = temp;
            dt.x += dx[i];
            dt.y += dy[i];
            if(dt.x >=0 && dt.x < m && dt.y >= 0 && dt.y<n && vis[dt.y][dt.x] == 1){
                vis[dt.y][dt.x] = 0;
                dt.step++;
                q.push(dt);
            }
        }
    }
}
int main()
{
    while(cin >> n >> m){
        int cnt;
        cnt = 0;
        bool vis[205][205];
        memset(vis,0,sizeof(vis[0][0])*205*205);
        memset(ans,0,sizeof(ans[0][0])*205*205);
        memset(res,0,sizeof(res[0][0])*205*205);
        memset(people,0,sizeof(people[0][0])*205*205);
        int Yy,Yx,My,Mx;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                char t;
                cin >> t;
                if(t == '.' || t == '@') vis[i][j] = 1;
                if(t == '@'){
                    ans[i][j] = 1;
                    cnt++;
                } 
                if(t == 'Y'){
                    Yy = i;
                    Yx = j;
                }
                if(t == 'M'){
                    My = i;
                    Mx = j;
                }
            }
        } 
        bool cpvis[205][205];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cpvis[i][j] = vis[i][j];
            }
        }
        bfs(Yx,Yy,vis,cnt);
        bfs(Mx,My,cpvis,cnt);
        int minest = INF;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(ans[i][j] == 1 && people[i][j] == 2){
                    if(minest > res[i][j]){
                        minest = res[i][j];
                    }
                }
            }
        }
        cout << minest*11 << endl;

    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值