我是靠谱客的博主 动听山水,最近开发中收集的这篇文章主要介绍Hdu 2612 Find a way(bfs) Find a way,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Find a way

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


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   |   We have carefully selected several similar problems for you:   2717  1254  1728  2102  1072 
 

题意:
求M和Y到达同一个‘@’所需要的时间总和最小,每步需要时间11分钟。

题解:
两次BFS求M和Y到每个‘@’之间的最小距离,之后求相加的最小值。


CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#define N 100010
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos(-1.0);
typedef long long ll;
const int INF=1000010;
using namespace std;
struct node
{
int x,y;
int num;//保存两人到@的最小值
int num2;
};
vector<node>q;
queue<node>que;
int n,m;
char mp[222][222];
bool vis[222][222];
int len,Min,l;
int Mx,My,Yx,Yy;
int xx[4]= {-1,0,1,0};
int yy[4]= {0,1,0,-1};
void bfs(int x,int y)
{
memset(vis,0,sizeof vis);
while(que.size())
que.pop();
node t,tt;
t.x=x,t.y=y;
t.num=0;
que.push(t);
int s=0;
while(que.size())
{
t=que.front();
que.pop();
if(mp[t.x][t.y]=='@')
{
for(int i=0; i<l; i++)//找到@
{
if(q[i].x==t.x&&q[i].y==t.y)
{
q[i].num=min(t.num,q[i].num);
break;
}
}
}
for(int i=0; i<4; i++)
{
tt.x=t.x+xx[i];
tt.y=t.y+yy[i];
tt.num=t.num+1;
if(tt.x>=0&&tt.x<n&&tt.y>=0&&tt.y<m&&mp[tt.x][tt.y]!='#'&&!vis[tt.x][tt.y])
{
que.push(tt);
vis[tt.x][tt.y]=1;
}
}
}
}
int main()
{
//freopen("test.in","r",stdin);
while(cin>>n>>m)
{
len=0;
q.clear();
node a;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
cin>>mp[i][j];
if(mp[i][j]=='@')
{
a.x=i,a.y=j,a.num=INF,a.num2=0;
q.push_back(a);
}
if(mp[i][j]=='Y')
{
Yx=i,Yy=j;
}
if(mp[i][j]=='M')
{
Mx=i,My=j;
}
}
l=q.size();
bfs(Mx,My);
for(int i=0; i<l; i++)//把num的值转给num2,num重新复制无穷大
{
q[i].num2=q[i].num;
q[i].num=INF;
}
bfs(Yx,Yy);
Min=INF;
for(int i=0; i<l; i++)
{
if(q[i].num2+q[i].num<Min)
Min=q[i].num+q[i].num2;
}
cout<<Min*11<<endl;
}
return 0;
}



最后

以上就是动听山水为你收集整理的Hdu 2612 Find a way(bfs) Find a way的全部内容,希望文章能够帮你解决Hdu 2612 Find a way(bfs) Find a way所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部