hdu 2612 Find a way

来源:互联网 发布:晋江 小说 推荐 知乎 编辑:程序博客网 时间:2024/06/10 22:47

Find a way

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 74   Accepted Submission(s) : 25

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

6688

66

两个人往kfc走,求走到后步数和最小,因为有很多kfc存在,就用2个数组记录2个人到不同kfc的答案,最后求和最小。wa了很多次,后来发现应该要把没走到的点步数记录为无限大

而不是0。。。

#include<iostream>#include<cstring>#include<queue>using namespace std;struct po{    int x,y;    int step;};int ren[2][2];int kfc[222][2];char ma[222][222];int n,m;int ans1[222][222];int ans2[222][222];int vis1[222][222];int vis2[222][222];int dx[4]={0,0,1,-1};int dy[4]={1,-1,0,0};bool check(int x,int y){  if(x<1||y<1||x>n||y>m||ma[x][y]=='#')  return 1;  return 0;}void bfs(int sx,int sy){    po tt,hh;    queue<po> st;    tt.x=sx;    tt.y=sy;    tt.step=0;    ans1[sx][sy]=0;    //memset(vis1,0,sizeof(vis1));    vis1[sx][sy]=1;    st.push(tt);    while(!st.empty())    {        tt=st.front();        st.pop();        for(int i=0;i<4;i++)        {            hh=tt;            hh.x+=dx[i];            hh.y+=dy[i];            hh.step+=1;            if(check(hh.x,hh.y))continue;            if(ans1[hh.x][hh.y])continue;            //vis1[hh.x][hh.y]=1;            ans1[hh.x][hh.y]=hh.step;            st.push(hh);        }    }}void bfs1(int sx,int sy){    po tt,hh;    queue<po> st;    tt.x=sx;    tt.y=sy;    tt.step=0;    ans2[sx][sy]=0;    memset(vis2,0,sizeof(vis2));    //vis2[sx][sy]=1;    st.push(tt);    while(!st.empty())    {        tt=st.front();        st.pop();        for(int i=0;i<4;i++)        {            hh=tt;            hh.x+=dx[i];            hh.y+=dy[i];            hh.step+=1;            if(check(hh.x,hh.y))continue;            if(ans2[hh.x][hh.y])continue;            //vis2[hh.x][hh.y]=1;            ans2[hh.x][hh.y]=hh.step;            st.push(hh);        }    }    }int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {            memset(ans1,0,sizeof(ans1));        memset(ans2,0,sizeof(ans2));        int js=0;        for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)        {            cin>>ma[i][j];            if(ma[i][j]=='Y')            {                ren[0][0]=i;                ren[0][1]=j;            }            if(ma[i][j]=='M')            {                ren[1][0]=i;                ren[1][1]=j;            }            }        bfs(ren[0][0],ren[0][1]);        bfs1(ren[1][0],ren[1][1]);        long long int ans=99999999;         for(int ss=1;ss<=n;ss++)         for(int sss=1;sss<=m;sss++)         {             if(ma[ss][sss]=='@')             {                 if(ans>ans1[ss][sss]+ans2[ss][sss]&&ans1[ss][sss]!=0&&ans2[ss][sss]!=0)                 ans=ans1[ss][sss]+ans2[ss][sss];             }         }        cout<<ans*11<<endl;    }    return 0;}

原创粉丝点击