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
 
题意,俩人要去kfc吃鸡,然后一个从Y出发,一个从M出发,到达同一个@(@代表kfc)

思路:两个bfs然后来记录用cnt记录所有的从起点的目前点的距离,然后加一下就是距离,最后求一发最小的就行

有个点必须要注意,开始的时候一定要把cnt初始化为inf,要不会出现这种情况

4 4

 
 
Y.#@
...#
.#..
@#.M

这样的话,我们是不能达到右上角的,如果cnt初始化为0,因为求最小值,那么结果就是0,而不是10.还有就是看了别人的博客,在y开始找的时候到底能不能过M有点迷茫

#include<stdio.h>
#include<queue>
#include<cstring>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
struct node
{
    int x,y;
    int step;
};
int cnt[205][205],vis[205][205],cnt1[205][205],cnt2[205][205];
int dx[4]= {1,0,-1,0};
int dy[4]= {0,1,0,-1};
char mapp[205][205];
int n,m,yx,yy,mx,my;
queue<node>q;
int judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m)
        return 1;
    return 0;
}
void bfs(int x,int y,int flag)
{
    memset(cnt,inf,sizeof(cnt));
    memset(vis,0,sizeof(vis));
    while(!q.empty())
        q.pop();
    node st;
    st.x=x;
    st.y=y;
    st.step=0;
    vis[x][y]=1;
    q.push(st);
    while(!q.empty())
    {
        node tmp=q.front();
        q.pop();
        cnt[tmp.x][tmp.y]=tmp.step;
        tmp.step++;
        node nextt;
        for(int i=0; i<4; i++)
        {
            nextt.x=tmp.x+dx[i];
            nextt.y=tmp.y+dy[i];
            nextt.step=tmp.step;
            if(judge(nextt.x,nextt.y)&&vis[nextt.x][nextt.y]==0&&mapp[nextt.x][nextt.y]!='#')
            {
                vis[nextt.x][nextt.y]=1;
                cnt[nextt.x][nextt.y]=nextt.step;
                q.push(nextt);
            }
        }
    }
    if(flag==1)
        memcpy(cnt1,cnt,sizeof(cnt));
    if(flag==2)
        memcpy(cnt2,cnt,sizeof(cnt));
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
//        memset(cnt1,0,sizeof(cnt1));
//        memset(cnt2,0,sizeof(cnt2));
        for(int i=0; i<n; i++)
        {
            scanf("%s",mapp[i]);
            for(int j=0; j<m; j++)
            {
                if(mapp[i][j]=='M')
                    mx=i,my=j;
                if(mapp[i][j]=='Y')
                    yx=i,yy=j;
            }
        }
//        mapp[mx][my]='#';
        bfs(yx,yy,1);
//        mapp[mx][my]='M';
//        mapp[yx][yy]='#';
        bfs(mx,my,2);
        int minn=inf;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                if(mapp[i][j]=='@')
                {
                    if(minn>(cnt1[i][j]+cnt2[i][j]))
                    {
                        minn=cnt1[i][j]+cnt2[i][j];
                    }
                }
            }
        printf("%d\n",minn*11);
    }
}

猜你喜欢

转载自blog.csdn.net/zezzezzez/article/details/80002867