Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10474 Accepted Submission(s): 3421
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
Author
yifenfei
题意:两人约在KFC店见面,但市里有很多家KFC店,每人走路的方向有四种选择,每当从一条路走到另一条路需要花费11分钟,选择一家KFC店使得两人花费的总时间最小。( BFS )
#include<stdio.h>
#include<queue>
#include<string.h>
#define M 100000000
using namespace std;
int n,m,flag,dis[210][210][2];
int vis[210][210];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; //代表四个方向
char s[210][210];
struct node
{
int x,y;
int step;
};
int go(int a,int b)
{
if(a>=0&&b>=0&&a<n&&b<m&&s[a][b]!='#')
return 1;
return 0;
}
void bfs(int x,int y)
{
queue<node>que;
node st,ed;
st.x=x;
st.y=y;
st.step=0;
vis[x][y]=1;
que.push(st);
while(!que.empty())
{
st=que.front();
que.pop();
if(s[st.x][st.y]=='@')
{
dis[st.x][st.y][flag]=st.step; //不能加 return @_@
}
for(int i=0;i<4;i++)
{
ed.x=st.x+dx[i];
ed.y=st.y+dy[i];
if(go(ed.x,ed.y)&&!vis[ed.x][ed.y])
{
vis[ed.x][ed.y]=1; //标记已经走过的路线
ed.step=st.step+1;
que.push(ed);
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
getchar();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%c",&s[i][j]);
dis[i][j][0]=dis[i][j][1]=M;
}
getchar();
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(s[i][j]=='Y')
{
flag=0;
memset(vis,0,sizeof(vis));
bfs(i,j);
}
else if(s[i][j]=='M')
{
flag=1;
memset(vis,0,sizeof(vis));
bfs(i,j);
}
}
}
int max=M;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(s[i][j]=='@'&&max>dis[i][j][0]+dis[i][j][1]) //求最短的时间
max=dis[i][j][0]+dis[i][j][1];
}
}
printf("%d\n",max*11);
}
}