链接:戳这里
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m;
char mp[220][220];
struct node{
int x,y,step;
node(int x=0,int y=0,int step=0):x(x),y(y),step(step){}
}anw[40010];
int cnt;
node S,T;
int vis[220][220];
int xx[4]={0,1,0,-1};
int yy[4]={1,0,-1,0};
bool pd(node t){
if(t.x<1 || t.x>n || t.y<1 || t.y>m || mp[t.x][t.y]=='#')
return false;
if(vis[t.x][t.y])
return false;
return true;
}
void BFS(node st,int num[][220]){
queue<node> qu;
mst(vis,0);
qu.push(st);
vis[st.x][st.y]=1;
num[st.x][st.y]=0;
while(!qu.empty()){
node now=qu.front(),next;
qu.pop();
for(int i=0;i<4;i++){
next.x=now.x+xx[i];
next.y=now.y+yy[i];
if(pd(next)){
next.step=now.step+1;
num[next.x][next.y]=next.step;
vis[next.x][next.y]=1;
qu.push(next);
}
}
}
}
int num1[220][220],num2[220][220];
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
cnt=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
num1[i][j]=1e9;
num2[i][j]=1e9;
}
}
for(int i=1;i<=n;i++){
getchar();
for(int j=1;j<=m;j++){
scanf("%c",&mp[i][j]);
if(mp[i][j]=='Y') S=node(i,j,0);
if(mp[i][j]=='M') T=node(i,j,0);
}
}
BFS(S,num1);
BFS(T,num2);
int ans=1e9;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(mp[i][j]=='@' && num1[i][j]!=1e9 && num2[i][j]!=1e9){
ans=min(ans,num1[i][j]+num2[i][j]);
}
}
}
if(ans==1e9) cout<<0<<endl;
else printf("%d\n",ans*11);
}
return 0;
}