hdu 2612 Find a way (BFS)

简介:

Find a way

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


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
 


题目大意:

就是 n 行 m 列,看Y和M到达@的时间的和最短就行,(注意可能有多个@)


解题思路:

就是简单的BFS,只是注意一下细节就行了,具体的都在代码里给出了解释。。。


上代码:

/*
2015 - 09 - 16 早上

Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 205;
const double eps = 1e-7;
bool vis[maxn][maxn];
const int dir[4][2]= {1, 0, 0, 1, -1, 0, 0, -1};
char map[maxn][maxn];

int m, n;
int tm[maxn][maxn], ty[maxn][maxn];///记录M到达任意KFC的时间,记录Y到达任意KFC的时间

struct node
{
    int x, y, step;
};
///判断出界条件
bool Judge(int x, int y)
{
    if(x<0 || y<0 || x>=n || y>=m || map[x][y]=='#' || vis[x][y])
        return false;
    return true;
}
///广搜
void bfs(int x, int y, int sum[][maxn])///sum -> 记录时间的数组
{
    //memset(sum, 0, sizeof(sum));
    queue<node>q;
    node start, end;
    start.x = x, start.y = y, start.step = 0;
    //vis[x][y] = true;
    q.push(start);
    while(!q.empty())
    {
        start = q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            end.x = start.x + dir[i][0];
            end.y = start.y + dir[i][1];
            if(Judge(end.x, end.y))
            {
                end.step = start.step+1;
                vis[end.x][end.y] = true;
                if(map[end.x][end.y] == '@')
                    sum[end.x][end.y] = end.step;
                q.push(end);
            }
        }
    }
}
int main()
{
    while(cin>>n>>m)
    {
        memset(tm, 0, sizeof(tm));
        memset(ty, 0, sizeof(ty));
        for(int i=0; i<n; i++)
        cin>>map[i];
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(map[i][j] == 'Y')
                {
                    memset(vis, false, sizeof(vis));
                    vis[i][j] = true;
                    bfs(i, j, ty);
                }
                if(map[i][j] == 'M')
                {
                    memset(vis, false, sizeof(vis));
                    vis[i][j] = true;
                    bfs(i, j, tm);
                }
            }
        }
        int Min = 999999;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                if(Min > ty[i][j]+tm[i][j] && ty[i][j] && tm[i][j])
                    Min = ty[i][j]+tm[i][j];
        printf("%d\n",Min*11);
    }
    return 0;
}


目录
相关文章
|
6月前
|
机器学习/深度学习 安全 Java
hdu-1596-find the safest road(dijkstra)
hdu-1596-find the safest road(dijkstra)
40 0
HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)
HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)
108 0
|
并行计算
Find a way(两个BFS)
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally.
1107 0
|
Java
HDU 2689 Sort it【树状数组】
Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4672    Accepted Submission(s): 3244 ...
1096 0
【HDU 4771 Stealing Harry Potter&#39;s Precious】BFS+状压
2013杭州区域赛现场赛二水。。。 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间。 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点,搜索下一个最近的宝藏,直至找到全部k个宝藏。
1040 0