ACM-steps--4.2.6--Find a way

16 篇文章 0 订阅

Find a way

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 159 Accepted Submission(s): 63
 
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
 
Author
yifenfei
 
Source
奋斗的年代
 
Recommend
yifenfei



#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
char map[209][209];//用于地图的输入。
int y_step[209][209];
int m_step[209][209];//用于记录y和m到达每一步所用的最小的步数;
int step[209][209];
//求解两人到达同一个地方,且两人都可以移动,求出两人分别到达每一个每一个目的地所用的最短路;
//相加求出时间,然后枚举出最短的时间。
int vis[209][209];//访问的状态来那个。
int y_curx,y_cury,m_curx,m_cury;//记录两个人最开始的坐标.
int n,m;//表示行列。
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
struct dyx
{
    int x,y;
};
int wyx(int x,int y)
{
    if(x<0||x>n||y<0||y>m||vis[x][y]||map[x][y]=='#')
    return 1;
    return 0;
}
int bfs(int now_x,int now_y)
{
    queue<dyx> que;
    dyx now,next;
    now.x=now_x;
    now.y=now_y;
    memset(vis,0,sizeof(vis));
    vis[now_x][now_y]=1;
    step[now_x][now_y]=0;
    que.push(now);
    while(!que.empty())
    {
        now=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            next=now;
            next.x+=dx[i];
            next.y+=dy[i];
            if(wyx(next.x,next.y))
            continue;
            step[next.x][next.y]=step[now.x][now.y]+1;
            vis[next.x][next.y]=1;
            que.push(next);
        }
    }
    return 0;
}
int main()
{
    //int i,j;
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>map[i][j];
                //首先寻找y和m的初始位置.
                if(map[i][j]=='Y')
                {
                    y_curx=i;
                    y_cury=j;
                }
                else if(map[i][j]=='M')
                {
                    m_curx=i;
                    m_cury=j;
                }
            }
        }
        int mmin=99999999;
        bfs(y_curx,y_cury);
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            y_step[i][j]=step[i][j];
        }
        bfs(m_curx,m_cury);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                m_step[i][j]=step[i][j];
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                step[i][j]=y_step[i][j]+m_step[i][j];
                if(map[i][j]=='@')
                {
                    if(mmin>step[i][j])
                    mmin=step[i][j];
                }
            }
        }
        cout<<mmin*11<<endl;//经过每一个格子都会耗费11分钟;
    }
    return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值