概述
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 31236 Accepted Submission(s): 9984
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
奋斗的年代
题意: 两个人在‘@’碰头,每走一格花费11min,求两人花费时间最短的‘@’用了多少时间。bfs用两次就可以。
AC code:
#include<bits/stdc++.h>
using namespace std;
char mp[210][210];
int v[210][210];
int d[210][210];
int dis[4][2]= {0,1,1,0,-1,0,0,-1};
int n,m;
struct node
{
int x;
int y;
int step;
};
void bfs(int x,int y,int step)
{
node first;
first.x = x;
first.y = y;
first.step = step;
v[x][y] =1;
queue<node> q;
q.push(first);
while(!q.empty())
{
node next;
first = q.front();
q.pop();
for(int i = 0; i<4; i++)
{
next.x = first.x + dis[i][0];
next.y = first.y + dis[i][1];
next.step = first.step+1;
int xx,yy;
xx = next.x;
yy = next.y;
if(xx>=0 && xx<n && yy>=0 && yy<m && v[xx][yy]==0 && mp[xx][yy]!='#')
{
if(mp[xx][yy]=='.')
{
v[xx][yy] = 1;
q.push(next);
}
if(mp[xx][yy]=='@')
{
v[xx][yy]=1;
d[xx][yy] = d[xx][yy]+next.step;
q.push(next);
}
}
}
}
return ;
}
int main ()
{
int x1,y1,x2,y2;
while(~scanf("%d%d",&n,&m))
{
getchar();
memset(d,0,sizeof(d));
memset(v,0,sizeof(v));
for(int i = 0; i<n; i++)
{
for(int j = 0; j<m; j++)
{
scanf("%c",&mp[i][j]);
if(mp[i][j]=='Y')
{
x1 = i;
y1 = j;
}
if(mp[i][j]=='M')
{
x2 = i;
y2 = j;
}
}
getchar();
}
bfs(x1,y1,0);
memset(v,0,sizeof(v));
bfs(x2,y2,0);
int ans = 40010;
for(int i = 0; i<n; i++)
{
for(int j = 0; j<m; j++)
{
if(d[i][j]!=0)
{
ans = min(ans,d[i][j]);
}
}
}
printf("%dn",ans*11);
}
return 0;
}
最后
以上就是淡然啤酒为你收集整理的HDU2612 Find a way 简单bfs的全部内容,希望文章能够帮你解决HDU2612 Find a way 简单bfs所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复