1. 程式人生 > 實用技巧 >hdu 2612 Find a way

hdu 2612 Find a way

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

題目大意

兩個人,一個M,一個Y,求兩個人KFC(@)的最短時間。

思路

很明顯的一道BFS的題,但是,出現了兩個起點,那麼可以進行兩次的BFS,分別存下兩個人的最短路徑。最後找到兩個人的最短路徑之和。

程式碼

#include<iostream>
#include<queue>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;

int fx[4][2] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
bool vis[205][205];
char map[205][205];
int dp[205][205][2];//分別記錄兩個人的最短路徑
int n, m;
int flag;//當flag=0時,dp[][][flag]記錄Y,當flag=1時,dp[][][flag]記錄m。
const int INF = 0x3f3f3f;

struct node
{
    int x, y, step;
};


void BFS(int x,int y){
    queue <node> q;
    node s, e;
    s.x = x;
    s.y = y;
    s.step = 0;
    vis[x][y] = true;
    q.push(s);
    while (!q.empty()){
        s = q.front();
        q.pop();
        for (int i = 0; i<4; i++)
        {
            e.x = s.x + fx[i][0];
            e.y = s.y + fx[i][1];
            if (e.x >= 0 && e.x<n&&e.y >= 0 && e.y<m&&!vis[e.x][e.y] && (map[e.x][e.y] == '@' || map[e.x][e.y] == '.'))
            {
                vis[e.x][e.y] = 1;
                e.step = s.step + 1;
                if (map[e.x][e.y] == '@')
                    dp[e.x][e.y][flag] = min(dp[e.x][e.y][flag], e.step);//留下最小的路程
                q.push(e);
            }
        }
    }
}

int main(){

    int i, j, cnt;
    while (cin >> n >> m){
        memset(dp, INF, sizeof(dp));
        for (i = 0; i<n; i++)
            scanf("%s", map[i]);
        for (i = 0; i < n; i++){
            for (j = 0; j < m; j++)
            {
                if (map[i][j] == 'Y')
                {
                    flag = 0;
                    memset(vis, 0, sizeof(vis));
                    BFS(i, j);
                }
                else if (map[i][j] == 'M')
                {
                    flag = 1;
                    memset(vis, 0, sizeof(vis));
                    BFS(i, j);
                }
            }
        }
        cnt = INF;
        for (i = 0; i < n; i++){
            for (j = 0; j<m; j++)
            {
                if (map[i][j] == '@'&&cnt>dp[i][j][0] + dp[i][j][1])
                    cnt = dp[i][j][0] + dp[i][j][1];
            }
        }
        printf("%d\n", cnt * 11);//cnt為最短路程,因為每步要11分鐘,即為cnt*11;
    }
    system("pause");
    return 0;
}