HDU - 2612 Find a way

Find a way

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


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加入到队列?然后搜到两个结束。没想出来。
最后只好屈服于题解。
做法是:依次将两个人加入到队列进行bfs,计算出每一个KFC离这两个人的距离。
再进行一个循环判断,哪个kfc离这两人的距离和最近。
有个坑点是,两个人的位置是不可以走的,这个题目里可能没说清。kfc是能走的。
//#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#define __max(a,b)  (((a) > (b)) ? (a) : (b))
#define __min(a,b)  (((a) < (b)) ? (a) : (b))
using namespace std;
const int maxn = 1e4;
const int N = 300;
bool Prime[maxn];
int perm[N][N][5];//记录时间的数组
char maze[N][N];
bool used[N][N];
long long n,k,cnt = 0;
long long ans = 0;
int t;
typedef pair<int,int> P;
int dx[] = {-1,1,0,0,0,0};
int dy[] = {0,0,1,-1,0,0};
int dz[] = {0,0,0,0,-1,1};
P father[10][10];
int flag = 0;
typedef long long ll;
int l,r;
void bfs(int x, int y,int mode)//mode表明是搜索的是小Y还是小M
{
    queue<P> Q;
    Q.push(P(x,y));
    perm[x][y][mode] = 0;
    used[x][y] = 1;
    while(Q.size()){
        P t = Q.front();
        Q.pop();
        for(int i = 0; i < 4; i++)
        {
            int px = dx[i] + t.first, py = dy[i] + t.second;
            if(px >= 0 && px < l && py >= 0 && py < r && used[px][py] == 0)
                if(maze[px][py] == '.' || maze[px][py] == '@')
                {
                    used[px][py] = 1;
                    perm[px][py][mode] = perm[t.first][t.second][mode] + 1;
                    Q.push(P(px,py));
                }
        }
        
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    while(cin>>l>>r)
    {
        for(int i = 0; i < l; i++)
            cin>>maze[i];
        for(int i = 0; i < l; i++)
            for(int j = 0; j < r; j++)
            {
                if(maze[i][j] == 'Y')
                {
                    memset(used,0,sizeof(used));
                    bfs(i,j,0);
                }
                if(maze[i][j] == 'M')
                {
                    memset(used,0,sizeof(used));
                    bfs(i,j,1);
                }
            }
        ans = 1 << 20;
        for(int i = 0; i < l; i++)
            for(int j = 0; j < r; j++)
                if(maze[i][j] == '@' && perm[i][j][0] > 0 && perm[i][j][1] > 0)//还需保证kfc能走到
                    ans = __min(ans,perm[i][j][0]+perm[i][j][1]);
        cout<<ans*11<<endl;
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值