Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2514 Accepted Submission(s): 804
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记录每个人到每个kfc所用的最小时间,然后从中选出两个相加为最小的。如果某个人去不到某一间kfc则标记这个时间为INFAC代码:#include <iostream> #include <cstring> #include <cstdio> #include <queue> #include <cmath> #include <string> #include <algorithm> #define INF 10000000 using namespace std; struct node { int x,y; int step; }yi,me; int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int m,n,flag; int dis[205][205][2]; char map[205][205]; bool v[205][205]; bool judge(int x,int y) { if(x>=0&&y>=0&&x<n&&y<m&&map[x][y]!='#'&&!v[x][y]) return true; return false; } void bfs(node peo) { node first,next; queue<node>q; memset(v,false,sizeof(v)); v[peo.x][peo.y]=true; peo.step=0; q.push(peo); while(!q.empty()) { first=q.front(); q.pop(); if(map[first.x][first.y]=='@') dis[first.x][first.y][flag]=first.step; for(int i=0;i<4;i++) { next=first; next.x+=d[i][0]; next.y+=d[i][1]; if(!judge(next.x,next.y)) continue; v[next.x][next.y]=true; next.step+=11; q.push(next); } } } int main() { while(cin>>n>>m) { for(int i=0;i<n;i++) for(int j=0;j<m;j++) { cin>>map[i][j]; if(map[i][j]=='Y') { yi.x=i; yi.y=j; } else if(map[i][j]=='M') { me.x=i; me.y=j; } } for(int i=0;i<n;i++) for(int j=0;j<m;j++) dis[i][j][0]=dis[i][j][1]=INF; flag=0; bfs(yi); flag=1; bfs(me); int min=INF; for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(map[i][j]=='@'&&min>dis[i][j][0]+dis[i][j][1]) min=dis[i][j][0]+dis[i][j][1]; cout<<min<<endl; } return 0; }