Find a way HDU

来源:互联网 发布:wind导出股票数据 编辑:程序博客网 时间:2024/06/09 16:01

Find a way

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


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
 

Source
奋斗的年代
 

Recommend
yifenfei
 

Statistic | Submit | Discuss | Note

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2612




思路:直接全图走一遍BFS,保存两人到每一家KFC的最短路,最后遍历所有KFC找出最短路


AC代码:

#include<cstdio>#include<queue>#include<cstring>using namespace std;int x1,y1,x2,y2;int n,m;int dx[]={1,-1,0,0};int dy[]={0,0,-1,1};struct point {    int x,y;};char map1[250][250];int anum[250][250];int bnum[250][250];int vis[250][250];void getindex(){    for(int i=0;i<n;i++){        for(int j=0;j<m;j++){            if(map1[i][j]=='Y'){                x1=i;                y1=j;            }            if(map1[i][j]=='M'){                x2=i;                y2=j;            }        }    }}void solve(point s,int flag){    memset(vis,0,sizeof(vis));    vis[s.x][s.y]=1;    queue<point> q;    q.push(s);    while(!q.empty()){        point f=q.front(); q.pop();        for(int i=0;i<4;i++){            point now;            now.x=f.x+dx[i];            now.y=f.y+dy[i];            if(now.x>=0&&now.x<n&&now.y>=0&&now.y<m&&!vis[now.x][now.y]&&map1[now.x][now.y]!='#'){                vis[now.x][now.y]=1;                q.push(now);                if(flag==1) anum[now.x][now.y]=anum[f.x][f.y]+1;                if(flag==0) bnum[now.x][now.y]=bnum[f.x][f.y]+1;            }        }    }}int main(){    while(scanf("%d%d",&n,&m)!=EOF){        //if(n<2||m<2) break;        for(int i=0;i<n;i++){            scanf("%s",map1[i]);        }        getindex();        point a; a.x=x1; a.y=y1;        point b; b.x=x2; b.y=y2;        memset(anum,0,sizeof(anum));        memset(bnum,0,sizeof(bnum));        solve(a,1);        solve(b,0);        int min1=100000;        memset(vis,0,sizeof(vis));        for(int i=0;i<n;i++){            for(int j=0;j<m;j++){                if(map1[i][j]=='@'&&!vis[i][j]){                   // printf("(%d, %d)---%d\n",i,j,anum[i][j]);                    //printf("(%d, %d)---%d\n",i,j,bnum[i][j]);                    if(anum[i][j]>0&&bnum[i][j]>0)                    min1=min(min1,anum[i][j]+bnum[i][j]);                    vis[i][j]=1;                }            }        }        printf("%d\n",min1*11);    }    return 0;}