HDU 2612 Find a way

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): 4478    Accepted Submission(s): 1525
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.
 
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

 
Author
yifenfei
 
Source
분투 의 시대
 
Recommend
yifenfei
두 번 BFS Y 부터 모든 KFC 까지 의 최 단 거 리 를 구하 고 M 에서 모든 KFC 까지 의 최 단 거리 플러스 와 최 단 거 리 를 구하 고 있 습 니 다.
KFC 가 BFS 를 두 번 구하 면 시간 이 초과 된다. BFS 횟수 는 n*2(n 은 KFC 수량)---당시 어떻게 생각 했 는 지 모 르 겠 지만,
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;
struct node
{
int x,y;
int dist;
};
bool cmp(int a,int b)
{
return a>b;
}
int n,m;
char mp[210][210];
int vis[210][210];
int dir[4][2]={1,0,0,1,0,-1,-1,0};
int val[210][210];
queue<node>q;
void bfs(node a)
{
while(!q.empty())q.pop();
cle(vis);
vis[a.x][a.y]=1;
node u;
u.dist=0;
u.x=a.x;
u.y=a.y;
q.push(u);
while(!q.empty())
{
u=q.front();q.pop();
if(mp[u.x][u.y]=='@')val[u.x][u.y]+=u.dist;
for(int i=0;i<4;i++)
{
node v;
v.x=u.x+dir[i][0];
v.y=u.y+dir[i][1];
if(!vis[v.x][v.y]&&mp[v.x][v.y]!='#'&&v.x<=n&&v.x>=1&&v.y<=m&&v.y>=1)
{
v.dist=u.dist+1;
vis[v.x][v.y]=1;
q.push(v);
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF)
{
cle(val);
node Y,M;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='M')M.x=i,M.y=j,M.dist=0;
if(mp[i][j]=='Y')Y.x=i,Y.y=j,Y.dist=0;
}
int ans=INF;
bfs(Y),bfs(M);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(val[i][j]!=0)
ans=min(ans,val[i][j]);
}
printf("%d
",ans*11);
}
return 0;
}

좋은 웹페이지 즐겨찾기