美文网首页
ACM 之 D - Find a way

ACM 之 D - Find a way

作者: Gadore千里 | 来源:发表于2016-08-05 20:48 被阅读63次

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

理解

这道题有几点很重要:
1. Y 和 M 站的地方是路,可以走。
2. KFC可能被墙围起来( @可能会被#围起来)。
3. 遍历地图的时候记得Y 和 M 分别进行标记。

代码部分

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int n,m,sum,mx,my,yx,yy,vis[201][201],vat[201][201],vism[201][201],visy[201][201];
char map[201][201];
struct point
{
    int x,y;
};
queue<point>p;
point mov[4]={{1,0},{-1,0},{0,1},{0,-1}};
point con={-1,0};
void fun(int a,int b)
{
    sum=0;
    while(!p.empty())p.pop();
    memset(vat,0,sizeof(vat));
    point q;
    q.x=a;q.y=b;
    p.push(con);
    p.push(q);
    while(p.size()>1)
    {
        if(p.front().x==-1)
        {
            p.push(p.front());
            sum++;
            p.pop();
            continue;
        }
        int x=p.front().x;
        int y=p.front().y;
        if(map[x][y]!='@')vis[x][y]=1;
        for(int i=0;i<4;i++)
        {
            point z;
            z.x=x+mov[i].x;
            z.y=y+mov[i].y;
            if(map[z.x][z.y]=='#'||z.x<0||z.x>=n||z.y<0||z.y>=m)continue;
            if(map[z.x][z.y]=='.'||map[z.x][z.y]=='M'||map[z.x][z.y]=='Y')
            {
                if(vis[z.x][z.y]==0)
                {
                    vis[z.x][z.y]=1;
                    p.push(z);continue;
                }
            }
            if(map[z.x][z.y]=='@'&&vat[z.x][z.y]==0)
            {
                if(map[a][b]=='Y')
                    visy[z.x][z.y]=1;
                if(map[a][b]=='M')
                    vism[z.x][z.y]=1;
                vat[z.x][z.y]=1;    
                p.push(z);
                vis[z.x][z.y]+=sum;
            }
        }
        p.pop();
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        memset(vis,0,sizeof(vis));
        memset(vism,0,sizeof(vism));
        memset(visy,0,sizeof(visy));
        for(int i=0;i<n;i++)
        {   for(int j=0;j<m;j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j]=='Y')
                    {yx=i;yy=j;}
                if(map[i][j]=='M')
                    {mx=i;my=j;}
            }getchar();
        }
        fun(yx,yy);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(vis[i][j]==1&&map[i][j]!='@')
                    vis[i][j]=0;
        fun(mx,my);
        int min=40001;
        for(int i=0;i<n;i++)
        {   for(int j=0;j<m;j++)
            {
                if(map[i][j]=='@'&&vism[i][j]!=0&&visy[i][j]!=0)
                    if(vis[i][j]<min)
                        min=vis[i][j];
            }
        }
        printf("%d\n",min*11);
    }
    return 0;
}

意见反馈 || 任何建议

联系我(新浪)
邮箱:qianlizhihao@gmail.com

相关文章

网友评论

      本文标题:ACM 之 D - Find a way

      本文链接:https://www.haomeiwen.com/subject/pbfksttx.html