HDUj2612(两个起点找到最近的目的地)

Find a way

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


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
 
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN=205;
struct Node{
    int a,b,s;
    Node(){}
    Node(int ca,int cb,int cs)
    {
        a=ca,b=cb,s=cs;
    }
};
char mp[MAXN][MAXN];
int vis[MAXN][MAXN];
int t[MAXN][MAXN];
int n,m;
int dy[4]={0,1,0,-1};
int dx[4]={1,0,-1,0};
void bfs(int sy,int sx)
{
    memset(vis,0,sizeof(vis));
    queue<Node> que;
    que.push(Node(sy,sx,0));
    vis[sy][sx]=1;
    while(!que.empty())
    {
        Node now=que.front();que.pop();
        if(mp[now.a][now.b]=='@')
        {
            t[now.a][now.b]+=now.s;
        }
        for(int i=0;i<4;i++)
        {
            int ny=now.a+dy[i];
            int nx=now.b+dx[i];
            if(0<=ny&&ny<n&&0<=nx&&nx<m&&!vis[ny][nx]&&mp[ny][nx]!='#')
            {
                vis[ny][nx]=1;
                que.push(Node(ny,nx,now.s+1));
            }    
        }
    }
}
int yY,xY;
int yM,xM;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(t,0,sizeof(t));
        for(int i=0;i<n;i++)
        {
            scanf("%*c");
            for(int j=0;j<m;j++)
            {
                scanf("%c",&mp[i][j]);
                if(mp[i][j]=='Y')
                {
                    yY=i;
                    xY=j;
                }
                if(mp[i][j]=='M')
                {
                    yM=i;
                    xM=j;
                }
            }
        }
        bfs(yY,xY);
        bfs(yM,xM);    
        int mn=0x3fffffff;
        for(int i=0;i<n;i++)    
            for(int j=0;j<m;j++)
                if(mp[i][j]=='@'&&t[i][j]!=0)//存在不可达的'@' 
                    mn=min(mn,t[i][j]*11);
        printf("%d
",mn);
    }
        
    return 0;
}
 
原文地址:https://www.cnblogs.com/program-ccc/p/5010892.html