hdoj 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): 3454    Accepted Submission(s): 1130


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
 

Author
yifenfei
 

Source

題目大意:求從M點和Y點到@的最短距離和。
解題思路:從M點進行一遍BFS,再從Y點進行一遍BFS,儲存到所有@的距離和,再枚舉相加求最小即可。
                  最大的坑在於,Y點和M點是不能走的。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N = 210;
const int INF = 0x3ffffff;

struct Point{
    int x, y;
    int step;
}start, fr, next;
char mp[N][N];
int vis[N][N];
int kfc[N][N][2];
int dir[4][2] = {1, 0, 0, 1, 0, -1, -1, 0};
int n, m;

void bfs(int k){
    queue<Point> Q;
    Q.push(start);
    while(!Q.empty()){
        fr = Q.front();
        Q.pop();
        if(mp[fr.x][fr.y] == '@')
            kfc[fr.x][fr.y][k] = fr.step;
        for(int i = 0; i < 4; i++){
            int xx = fr.x + dir[i][0];
            int yy = fr.y + dir[i][1];
            if(mp[xx][yy] == '#' || mp[xx][yy] == '\0' || mp[xx][yy] == 'Y' || mp[xx][yy] == 'M') continue;
            if(!vis[xx][yy]){
                vis[xx][yy] = 1;
                next.x = xx;
                next.y = yy;
                next.step = fr.step + 1;
                Q.push(next);
            }
        }
    }
    return;
}

int main(){
    while(scanf("%d%d", &n, &m) != EOF){
        for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++)
            kfc[i][j][0] = kfc[i][j][1] = INF;
        memset(mp, '\0', sizeof(mp));
        for(int i = 1; i <= n; i++){
            scanf("%s", mp[i] + 1);
        }
        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++){
            if(mp[i][j] == 'Y'){
                start.x = i;
                start.y = j;
                start.step = 0;
                memset(vis, 0, sizeof(vis));
                vis[i][j] = 1;
                bfs(0);
            }
            else if(mp[i][j] == 'M'){
                start.x = i;
                start.y = j;
                start.step = 0;
                memset(vis, 0, sizeof(vis));
                vis[i][j] = 1;
                bfs(1);
            }
        }
        int mincost = INF;
        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++){
            if(mp[i][j] == '@'){
                mincost = min(mincost, (kfc[i][j][0] + kfc[i][j][1]) * 11);
            }
        }
        printf("%d\n", mincost);
    }
    return 0;
}

/*
5 5
Y..#@
...M.
...##
.....
@....
5 5
Y@@@@
@@@@@
@@@@@
@@@@@
@@@@M
*/



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章