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
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. #...#
88
66
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define INF 0xffffff
using namespace std;
int n,m;
char map[212][212];
int vis[212][212];
int dis[212][212][2];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int flag;
struct node
{
int x,y,s;
};
void BFS(int x,int y)
{
int i;
queue<struct node>q;
struct node t,f;
t.x=x;
t.y=y;
t.s=0;
vis[x][y]=1;
q.push(t);
while(!q.empty())
{
t=q.front();
q.pop();
f.s=t.s+1;
for(i=0;i<=3;i++)
{
f.x=t.x+dir[i][0];
f.y=t.y+dir[i][1];
if(f.x>=0&&f.x<=n-1&&f.y>=0&&f.y<=m-1&&map[f.x][f.y]!='#'&&vis[f.x][f.y]==0)
{
vis[f.x][f.y]=1;
if(map[f.x][f.y]=='@')
dis[f.x][f.y][flag]=f.s;
q.push(f);
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int i,j;
for(i=0;i<=n-1;i++)
{
scanf("%s",map[i]);
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=m-1;j++)
{
dis[i][j][0]=dis[i][j][1]=INF;
}
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=m-1;j++)
{
if(map[i][j]=='Y')
{
flag=0;
memset(vis,0,sizeof(vis));
BFS(i,j);
}
else if(map[i][j]=='M')
{
flag=1;
memset(vis,0,sizeof(vis));
BFS(i,j);
}
}
}
int min=INF;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=m-1;j++)
{
if(map[i][j]=='@'&&min>dis[i][j][0]+dis[i][j][1])
min=dis[i][j][0]+dis[i][j][1];
}
}
printf("%d\n",min*11);
}
return 0;
}
634



被折叠的 条评论
为什么被折叠?



