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.
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
题意
J和M想去一个共同的@,求最近他俩到@距离和*11;
思路
bfs,注意的是@和两个起点是可以路过的(我这样做AC了)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <cstring>
using namespace std;
#define inf 0x3f3f3f3f
char a[220][220];
int vis[220][220];
int n,m,i,j;
int x1,x2,yy,y2;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
struct node
{
int x,y,step;
int minx,miny;
};
node p[220][220];
int bfs()
{
queue<node> q;
node u,v;
u.x=x1,u.y=yy;
u.step=0;
q.push(u);
memset(vis,0,sizeof(vis));
vis[x1][yy] = 1;
while(!q.empty())
{
u = q.front();
q.pop();
if(a[u.x][u.y]=='@')
{
p[u.x][u.y].minx = min(p[u.x][u.y].minx,u.step);
}
for(i=0;i<4;i++)
{
int tx = u.x+dx[i];
int ty = u.y+dy[i];
if(tx<1||ty<1||tx>n||ty>m||a[tx][ty]=='#') continue;
if(!vis[tx][ty])
{
vis[tx][ty] = 1;
v.x = tx,v.y = ty;
v.step = u.step+1;
q.push(v);
}
}
}
u.x=x2,u.y=y2;
u.step=0;
q.push(u);
memset(vis,0,sizeof(vis));
vis[x2][y2] = 1;
while(!q.empty())
{
u = q.front();
q.pop();
if(a[u.x][u.y]=='@')
{
p[u.x][u.y].miny = min(p[u.x][u.y].miny,u.step);
}
for(i=0;i<4;i++)
{
int tx = u.x+dx[i];
int ty = u.y+dy[i];
if(tx<1||ty<1||tx>n||ty>m||a[tx][ty]=='#') continue;
if(!vis[tx][ty])
{
vis[tx][ty] = 1;
v.x = tx,v.y = ty;
v.step = u.step+1;
q.push(v);
}
}
}
int ans = inf;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(a[i][j]=='@')
{
ans = min(ans,p[i][j].minx+p[i][j].miny);
}
}
}
return ans*11;
}
int main()
{
while(cin>>n>>m)
{
for(i=1;i<=n;i++)
{
getchar();
for(j=1;j<=m;j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='Y')
{
x1=i,yy=j;
}
if(a[i][j]=='M')
{
x2=i,y2=j;
}
if(a[i][j]=='@')
{
p[i][j].minx = inf;
p[i][j].miny = inf;
}
}
}
int ans = bfs();
cout<<ans<<endl;
}
return 0;
}