这道题目需要用到两次广搜,分别找出两个人到各个kfc花费的时间(有的人可能无法到达所有的kfc,而且只要不是#就都能走)
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.
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.
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
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
66 88 66
#include<stdio.h>
#include<string.h >
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct list
{
int x;
int y;
int step;
};
int n,m;
bool check(int x,int y);
bool book[205][205];
char fig[205][205];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
vector<struct list> vy,vm;
queue<struct list> q;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int _min;
struct list Y,M;
vy.clear();
vm.clear();
memset(fig,0,sizeof(fig));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf(" %c",&fig[i][j]);
if(fig[i][j]=='Y')
{
Y.x=i;
Y.y=j;
Y.step=0;
}
if(fig[i][j]=='M')
{
M.x=i;
M.y=j;
M.step=0;
}
}
}
struct list ne;
q.push(Y);
memset(book,false,sizeof(book));
book[Y.x][Y.y]=true;
while(!q.empty())
{
struct list Q;
Q=q.front();
for(int i=0;i<4;i++)
{
if(check(Q.x+dir[i][0],Q.y+dir[i][1]))
{
book[Q.x+dir[i][0]][Q.y+dir[i][1]]=true;
ne.x=Q.x+dir[i][0];
ne.y=Q.y+dir[i][1];
ne.step=Q.step+1;
q.push(ne);
if(fig[Q.x+dir[i][0]][Q.y+dir[i][1]]=='@')
{
vy.push_back(ne);
}
}
}
q.pop();
}
q.push(M);
memset(book,false,sizeof(book));
book[M.x][M.y]=true;
while(!q.empty())
{
struct list Q;
Q=q.front();
for(int i=0;i<4;i++)
{
if(check(Q.x+dir[i][0],Q.y+dir[i][1]))
{
book[Q.x+dir[i][0]][Q.y+dir[i][1]]=true;
ne.x=Q.x+dir[i][0];
ne.y=Q.y+dir[i][1];
ne.step=Q.step+1;
q.push(ne);
if(fig[Q.x+dir[i][0]][Q.y+dir[i][1]]=='@')
{
vm.push_back(ne);
}
}
}
q.pop();
}
_min=0x3f3f3f3f;
for(int i=0;i<vy.size();i++)
{
for(int j=0;j<vm.size();j++)
{
if(vy[i].x==vm[j].x&&vy[i].y==vm[j].y)
_min=min(_min,vy[i].step+vm[j].step);
}
}
printf("%d\n",_min*11);
}
return 0;
}
bool check(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m)
if(book[x][y]==false)
if(fig[x][y]!='#')
return true;
return false;
}