题目:
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11589 Accepted Submission(s): 3771
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
Author
yifenfei
Source
奋斗的年代
Recommend
代码:
#include
#include
#include
#include
#include
#include
#include
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int go[4][2]= {{1,0},{-1,0},{0,-1},{0,1}};
int x1,x2,y1,y2;
char map[250][250];
int f1[250][250];//第一个人的步数
int f2[250][250];//第二个人的步数
int vis[250][250];//做标记
int n,m;
struct node
{
int x,y,s;
};
void bfs(int x,int y,int a[][250])//这里传入了第三个变量
{
node now,to;
queueq;
now.x=x,now.y=y,now.s=0;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=0; i<4; i++)//循环四个方向
{
int xx=now.x+go[i][0];
int yy=now.y+go[i][1];
if(xx>=0&&yy>=0&&xxf1[i][j]+f2[i][j]&&f1[i][j]&&f2[i][j])//有值的地方说明有kfc,找出最短的
min1=f1[i][j]+f2[i][j];
printf("%d\n",min1*11);
}
return 0;
}
本题的题意是Y,和M两个人要走到“@”这个地方,求的是他们走到哪一个“@”这个用的时间最短
'Y' 表示1所在的初始位置
'M' 表示2所在的初始位置
'#' 该点禁止通行
'.' 该点可通行
'@' KFC
思路是先找出这两个人的坐标,然后把它们到每一个目标点的时间算出来并且存下来(用f1,f2两个数组),最后用循环找出所用时间最短的