您的位置:首页 > 其它

N - Find a way HDU 2612 基础BFS

2015-03-27 12:54 441 查看
N - Find a way

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 2612

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<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=205;
const int inf=200000;
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define For(i,n) for(int i=0;i<(n);i++)
template<class T>inline T read(T&x)
{
char c;
while((c=getchar())<=32);
bool ok=false;
if(c=='-')ok=true,c=getchar();
for(x=0; c>32; c=getchar())
x=x*10+c-'0';
if(ok)x=-x;
return x;
}
template<class T> inline void read_(T&x,T&y)
{
read(x);
read(y);
}
template<class T> inline void write(T x)
{
if(x<0)putchar('-'),x=-x;
if(x<10)putchar(x+'0');
else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
write(x);
putchar('\n');
}
//  -------IO template------
int n,m;
struct node
{
int x,y;
node(int a,int b)
{
x=a;y=b;
}
};

char a[maxn][maxn];
int vis[maxn][maxn];

int dx[]={-1,1,0,0};
int dy[]={0,0,1,-1};
void bfs(int x,int y)
{
queue<node> q;
q.push(node(x,y));
memset(vis,0,sizeof(vis));
vis[x][y]=0;
while(!q.empty())
{
node s=q.front();q.pop();
For(i,4)
{
int xx=dx[i]+s.x;
int yy=dy[i]+s.y;
if(xx<0||xx>=n||yy<0||yy>=m||vis[xx][yy]||a[xx][yy]=='#'||a[xx][yy]=='Y'||a[xx][yy]=='M')continue;
vis[xx][yy]=vis[s.x][s.y]+1;
q.push(node(xx,yy));
}
}
}
int d[maxn][maxn];
void bfs2(int x,int y)
{
queue<node> q;
q.push(node(x,y));
memset(d,0,sizeof(d));
d[x][y]=0;
while(!q.empty())
{
node s=q.front();q.pop();
For(i,4)
{
int xx=dx[i]+s.x;
int yy=dy[i]+s.y;
if(xx<0||xx>=n||yy<0||yy>=m||d[xx][yy]||a[xx][yy]=='#'||a[xx][yy]=='Y'||a[xx][yy]=='M')continue;
d[xx][yy]=d[s.x][s.y]+1;
q.push(node(xx,yy));
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int i,j,k,t;
while(~scanf("%d%d",&n,&m))
{
int yi,yj,mi,mj;
For(i,n)
{
scanf("%s",a[i]);
For(j,m)
{
if(a[i][j]=='Y')
{
yi=i;
yj=j;
}
else if(a[i][j]=='M')
{
mi=i;
mj=j;
}
}
}
bfs(yi,yj);
bfs2(mi,mj);
int ans=inf;
For(i,n)For(j,m)if(a[i][j]=='@')
{
int rec=inf;
if(vis[i][j]&&d[i][j])
rec=vis[i][j]+d[i][j];
ans=min(rec,ans);
}
writeln(ans*11);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: