题目链接:http://hdu.hustoj.com/showproblem.php?pid=2612
题目大意:小Y和小M在@点见面,.为可走的路,#为不可走的路,问两个人的路程和最短花费时间
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两次,分别记录两个人到所有点的最短路径,最后两个for,check一下答案
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;
int n,m;
char mp[205][205];
int step1[205][205];
int step2[205][205];
int dir[4][2]={1,0,0,1,-1,0,0,-1};
int vis[205][205];
struct node
{
int x,y;
int step;
};
bool check(int a,int b)
{
if(a<=0||a>n||b<=0||b>m||vis[a][b]==1||mp[a][b]=='#')
return false;
return true;
}
void bfs1(int a,int b)
{
node now,next;
MEM(vis,0);
now.x=a;
now.y=b;
now.step=0;
step1[a][b]=0;
vis[a][b]=1;
queue<node>q;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=0;i<4;i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
next.step=now.step+1;
if(check(next.x,next.y))
{
q.push(next);
step1[next.x][next.y]=next.step;
vis[next.x][next.y]=1;
}
}
}
}
void bfs2(int a,int b)
{
node now,next;
MEM(vis,0);
now.x=a;
now.y=b;
now.step=0;
step2[a][b]=0;
vis[a][b]=1;
queue<node>q;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=0;i<4;i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
next.step=now.step+1;
if(check(next.x,next.y))
{
q.push(next);
step2[next.x][next.y]=next.step;
vis[next.x][next.y]=1;
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
std::ios::sync_with_stdio(false);
while(~scanf("%d%d",&n,&m))
{
int x1,y1,x2,y2;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf(" %c",&mp[i][j]);
if(mp[i][j]=='Y')
{
x1=i;
y1=j;
}
if(mp[i][j]=='M')
{
x2=i;
y2=j;
}
}
}
bfs1(x1,y1);
bfs2(x2,y2);
int ans=INF;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mp[i][j]=='@')
ans=min(ans,step1[i][j]+step2[i][j]);
}
}
printf("%d\n",ans*11);
}
return 0;
}