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.
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 [email protected] .#... .#... @..M. #...#
Sample Output
66 88 66
Author
yifenfei
Source
Recommend
yifenfei | We have carefully selected several similar problems for you: 1254 1175 2579 1983 1195
ac代码
#include<stdio.h> #include<string.h> #include<queue> #include<iostream> #define min(a,b) (a>b?b:a) #define INF 0xfffffff using namespace std; int vis1[1010][1010],ans1[1010][1010],ans2[1010][1010],n,m,vis2[1010][1010]; char map[1010][1010]; struct s { int x,y,step; }a,temp; int dx[4]={0,1,0,-1}; int dy[4]={1,0,-1,0}; int jud(struct s a,int vis[1010][1010]) { if(a.x<0||a.x>=n) return 0; if(a.y<0||a.y>=m) return 0; if(vis[a.x][a.y]) return 0; if(map[a.x][a.y]=='#') return 0; return 1; } void bfs(int x,int y,int ans[1010][1010],int vis[1010][1010]) { a.x=x; a.y=y; a.step=0; vis[x][y]=1; ans[x][y]=a.step; queue<struct s>q; q.push(a); while(!q.empty()) { a=q.front(); q.pop(); for(int i=0;i<4;i++) { temp.x=a.x+dx[i]; temp.y=a.y+dy[i]; if(!jud(temp,vis)) continue; temp.step=a.step+1; ans[temp.x][temp.y]=temp.step; vis[temp.x][temp.y]=1; q.push(temp); } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { int i,j,x1,x2,y1,y2; for(i=0;i<n;i++) { scanf("%s",map[i]); for(j=0;j<m;j++) { if(map[i][j]=='M') { x1=i; y1=j; } if(map[i][j]=='Y') { x2=i; y2=j; } } } memset(vis1,0,sizeof(vis1)); memset(ans1,0,sizeof(ans1)); bfs(x1,y1,ans1,vis1); memset(vis2,0,sizeof(vis2)); memset(ans2,0,sizeof(ans2)); bfs(x2,y2,ans2,vis2); int ans=INF; for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(map[i][j]=='@'&&vis1[i][j]&&vis2[i][j]) { //printf("%d %d\n",ans1[i][j],ans2[i][j]); ans=min(ans,ans1[i][j]+ans2[i][j]); } } } printf("%d\n",ans*11); } }