Find a way
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
题意:两个人去最近(相较两人综合而言)的麦当劳碰头
题型:广搜
思路:用人去搜麦当劳,第一次写的时候用麦当劳搜人超时
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
char map[210][210];
int n,m,a[40010][2],to[4][2]={0,1,1,0,0,-1,-1,0},book[210][210],b[210][210],sum[210][210];
struct dd
{
int x,y,s;
}h,d;
queue<dd> q;
void bfs(int x,int y)
{
int f=0,i;memset(book,0,sizeof(book));
h.x=x;h.y=y;h.s=0;book[x][y]=1;
q.push(h);
while(!q.empty())
{
h=q.front();
q.pop();
if(map[h.x][h.y]=='@')
sum[h.x][h.y]+=h.s,b[h.x][h.y]++;
for(i=0;i<4;i++)
{
d.x=h.x+to[i][0];
d.y=h.y+to[i][1];
d.s=h.s+1;
if(d.x<0||d.y<0||d.x>=n||d.y>=m||book[d.x][d.y]||map[d.x][d.y]=='#')continue;
book[d.x][d.y]=1;
q.push(d);
}
}
}
int main()
{
int minx,i,j,z,yx,yy,mx,my;
while(~scanf("%d%d",&n,&m))
{
for(i=0;i<n;i++)
scanf("%s",map[i]);
for(z=i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(map[i][j]=='Y')
yx=i,yy=j;
if(map[i][j]=='M')
mx=i,my=j;
if(map[i][j]=='@')
a[z][0]=i,a[z++][1]=j;
}
minx=0x3f3f3f3f;
memset(sum,0,sizeof(sum));memset(b,0,sizeof(b));
bfs(yx,yy);bfs(mx,my);
for(i=0;i<z;i++)
if(b[a[i][0]][a[i][1]]==2)//筛选出来两人都能到的麦当劳
minx=min(minx,sum[a[i][0]][a[i][1]]);
printf("%d\n",minx*11);
}
return 0;
}