Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4772 Accepted Submission(s): 1624
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#include<stdio.h> #include<string.h> #include<queue> using namespace std; #define min(a,b)(a>b?b:a) char map[1010][1010]; int vis1[1010][1010],vis2[1010][1010],ans1[1010][1010],ans2[1010][1010]; int m,n; int dx[4]={1,0,0,-1}; int dy[4]={0,1,-1,0}; struct node { int x,y; int step; }p,temp; int judge(node s,int vis[1010][1010]) { if(s.x<0||s.x>=m||s.y<0||s.y>=n) return 1; if(vis[s.x][s.y]) return 1; if(map[s.x][s.y]=='#') return 1; return 0; } void dfs(int x,int y,int ans[1010][1010],int vis[1010][1010]) { p.x=x; p.y=y; p.step=0; vis[x][y]=1; ans[x][y]=p.step; queue<node>q; q.push(p); while(!q.empty()) { p=q.front(); q.pop(); for(int i=0;i<4;i++) { temp.x=p.x+dx[i]; temp.y=p.y+dy[i]; if(judge(temp,vis)) continue; temp.step=p.step+1; ans[temp.x][temp.y]=temp.step; q.push(temp); vis[temp.x][temp.y]=1; } } } int main() { while(scanf("%d%d",&m,&n)!=EOF) { int i,j,x1,y1,x2,y2; for(i=0;i<m;i++) { scanf("%s",map[i]); for(j=0;j<n;j++) { if(map[i][j]=='Y') { x1=i;y1=j; } if(map[i][j]=='M') { x2=i;y2=j; } } } memset(vis1,0,sizeof(vis1)); memset(ans1,0,sizeof(ans1)); dfs(x1,y1,ans1,vis1); memset(vis2,0,sizeof(vis2)); memset(ans2,0,sizeof(ans2)); dfs(x2,y2,ans2,vis2); int ans=0xfffffff; for(i=0;i<m;i++) for(j=0;j<n;j++) { if(map[i][j]=='@'&&vis1[i][j]&&vis2[i][j]) { ans=min(ans,ans1[i][j]+ans2[i][j]); } } printf("%d ",ans*11); } return 0; }