码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2612 Find a way (BFS)

时间:2015-07-26 00:22:13      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

Find a way

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


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
 
题意:Y和M两人约定在@处见面,问两个人加起来的最短时间是多少
分析:BFS
没必要对每一个@进行BFS,以Y和M为起点进行BFS然后记录下到每个@的时间再把两者进行相加即可
技术分享
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<stdlib.h>
#include<algorithm>
#define LL __int64
using namespace std;
const int MAXN=200+5;
const int INF=0x3f3f3f3f;
int n,m,sx,sy,ex,ey,ans;
int vis[MAXN][MAXN][2];
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
char mat[MAXN][MAXN];
struct node
{
    int x,y;
}s;

bool ok(int x,int y)
{
    if(0<=x && x<n && 0<=y && y<m) return true;
    else return false;
}
void BFS(int x,int y,int opt)
{
    s.x=x;
    s.y=y;
    vis[x][y][opt]=1;
    queue<node> Q;
    Q.push(s);
    while(!Q.empty())
    {
        node now,next;
        now=Q.front();Q.pop();
        for(int i=0;i<4;i++)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            if(ok(next.x,next.y) && !vis[next.x][next.y][opt] && mat[next.x][next.y]!=# && mat[next.x][next.y]!=Y && mat[next.x][next.y]!=M)
            {
                vis[next.x][next.y][opt]=vis[now.x][now.y][opt]+1;
                Q.push(next);
            }
        }
    }
    return ;
}
int main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        memset(mat,0,sizeof(mat));
        for(int i=0;i<n;i++)
        {
            scanf("%s",mat[i]);
            for(int j=0;j<m;j++)
            {
                if(mat[i][j]==Y)
                    sx=i,sy=j;
                if(mat[i][j]==M)
                    ex=i,ey=j;
            }
        }
        memset(vis,0,sizeof(vis));
        BFS(sx,sy,0);
        BFS(ex,ey,1);
        ans=INF;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(vis[i][j][0] && vis[i][j][1] && mat[i][j]==@)
                {
                    int tmp=(vis[i][j][0]-1)+(vis[i][j][1]-1);
                    if(tmp<ans) ans=tmp;
                }
            }
        }

        printf("%d\n",ans*11);
    }
    return 0;
}
View Code

 

HDU 2612 Find a way (BFS)

标签:

原文地址:http://www.cnblogs.com/clliff/p/4676858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!