hdu 【2612】 Find a way

来源:互联网 发布:网络信息安全测评认证 编辑:程序博客网 时间:2024/06/05 13:22

Find a way

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


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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
 

Sample Output
668866
 

Author
yifenfei


#include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace std;const int maxn = 205;const int INF = 0x3f3f3f3f;int n,m,sx,sy,ex,ey;char maze[maxn][maxn];int vis[maxn][maxn];int time[maxn][maxn];int dir[4][2] = {{1,0},{-1,0},{0,-1},{0,1}};struct Node{    int x,y,t;}pre,cur;void bfs(int x,int y){    memset(vis,0,sizeof(vis));    queue <Node> que;    while(!que.empty()) que.pop();    pre.x = x;    pre.y = y;    pre.t = 0;    vis[x][y] = 1;    que.push(pre);    while(!que.empty())    {        pre = que.front(); que.pop();        if(maze[pre.x][pre.y] == '@')            time[pre.x][pre.y] += pre.t;        for(int i = 0; i < 4; i++)        {            int xx = pre.x + dir[i][0];            int yy = pre.y + dir[i][1];            if(xx < 0 || xx >= n || yy < 0 || yy >= m)                continue;            if(!vis[xx][yy] && maze[xx][yy] != '#')            {                vis[xx][yy] = 1;                cur.x = xx;                cur.y = yy;                cur.t = pre.t+11;                que.push(cur);            }        }    }}int main(){    while(scanf("%d%d", &n, &m) != EOF)    {        memset(time,0,sizeof(time));        for(int i = 0; i < n; i++)            scanf("%s", maze[i]);        for(int i = 0; i < n; i++)            for(int j = 0; j < m; j++)                if(maze[i][j] == 'Y')                    sx = i, sy = j;                else if(maze[i][j] == 'M')                    ex = i, ey = j;        bfs(sx,sy);        bfs(ex,ey);        int Min = INF;        for(int i = 0; i < n; i++)            for(int j = 0; j < m; j++)                if(time[i][j] && time[i][j] < Min)                    Min = time[i][j];        printf("%d\n", Min);    }    return 0;}


0 0