http://acm.hdu.edu.cn/showproblem.php?pid=2612
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14252 Accepted Submission(s): 4535
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.
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
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
思路:(刚开始我枚举要去的@结果超时了),从两个人的出发点分别bfs,找到这个人到所有@的时间记录下来。然后枚举@看哪一个用的时间少。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
using namespace std;
const int mod = 1e7+7;
const int INF = 1e8;
const int inf = 0x3f3f3f3f;
const int maxx = 100100;
const int N = 210;
int n,m;
char ma[N][N];
int v[N][N];
struct node
{
int x,y,step;
} stay,stam;
int diy[N][N];//记录y到每个@的距离
int dim[N][N];//记录m到每个@的距离
int dx[5]= {0,0,1,-1};
int dy[5]= {1,-1,0,0};
node p1,p2;
void bfs(node p1,int s)//bfs模板
{
memset(v,0,sizeof(v));
queue<node>q;
while(!q.empty())
q.pop();
p1.step=0;
v[p1.x][p1.y]=1;
q.push(p1);
while(!q.empty())
{
p1=q.front();
q.pop();
for(int i=0; i<4; i++)
{
p2=p1;
p2.x+=dx[i];
p2.y+=dy[i];
if(p2.x<0||p2.y<0||p2.x>=n||p2.y>=m)continue;
if(ma[p2.x][p2.y]!='#'&&!v[p2.x][p2.y])
{
v[p2.x][p2.y]=1;
p2.step++;
if(ma[p2.x][p2.y]=='@')
{
if(s)
diy[p2.x][p2.y]=p2.step;
else
dim[p2.x][p2.y]=p2.step;
}
q.push(p2);
}
}
}
return ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(diy,inf,sizeof(diy));
memset(dim,inf,sizeof(dim));//初始化为无穷大
for(int i=0; i<n; i++)
{
scanf("%s",ma[i]);
for(int j=0; j<m; j++)
{
if(ma[i][j]=='Y')
{
stay.x=i;
stay.y=j;
}
if(ma[i][j]=='M')
{
stam.x=i;
stam.y=j;
}
}
}
bfs(stay,1);
bfs(stam,0);
int ans=inf;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(ma[i][j]=='@')
ans=min(ans,diy[i][j]+dim[i][j]);//枚举@更新最小值
printf("%d\n",ans*11);
}
}