Find a way
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 137 Accepted Submission(s) : 41
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
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
Sample Input
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
Sample Output
66 8866
#include<iostream> #include<algorithm> #include<string.h> #include<queue> #include<cstdio> int n,m; char a[201][201],d[4][2]= {{1,0},{-1,0},{0,1},{0,-1}}; using namespace std; struct node { int x,y,step; }; int main() { while(scanf("%d%d",&n,&m)!=EOF) { bool b[201][201]; int c1[201][201],c2[201][201]; memset(b,0,sizeof(b)); for(int i=0; i<n; i++) scanf("%s",&a[i]); int x1,y1,x2,y2; for(int i=0; i<n; i++) for(int j=0; j<m; j++) { if(a[i][j]=='Y') x1=i,y1=j,a[i][j]=='.'; if(a[i][j]=='M') x2=i,y2=j,a[i][j]=='.'; } for(int i=0; i<n; i++) for(int j=0; j<m; j++) c1[i][j]=1000000,c2[i][j]=1000000; queue<node> P; node dd; dd.x=x1,dd.y=y1,dd.step=0; b[x1][y1]=1; P.push(dd); while(!P.empty()) { node dc=P.front(); P.pop(); for(int i=0; i<4; i++) { int xx=dc.x+d[i][0],yy=dc.y+d[i][1],step2=dc.step+11; if(xx>=0&&xx<n&&yy>=0&&yy<m) { if(a[xx][yy]=='@'&&b[xx][yy]==0) { c1[xx][yy]=step2,b[xx][yy]=1; node df; df.x=xx,df.y=yy,df.step=step2; P.push(df); } if(a[xx][yy]=='.'&&b[xx][yy]==0) { node dr; dr.x=xx,dr.y=yy,dr.step=step2; P.push(dr); b[xx][yy]=1; } } } } dd.x=x2,dd.y=y2,dd.step=0; P.push(dd); memset(b,0,sizeof(b)); b[x2][y2]=1; while(!P.empty()) { node dc=P.front(); P.pop(); for(int i=0; i<4; i++) { int xx=dc.x+d[i][0],yy=dc.y+d[i][1],step2=dc.step+11; if(xx>=0&&xx<n&&yy>=0&&yy<m) { if(a[xx][yy]=='@'&&b[xx][yy]==0) { c2[xx][yy]=step2,b[xx][yy]=1; node df; df.x=xx,df.y=yy,df.step=step2; P.push(df); } if(a[xx][yy]=='.'&&b[xx][yy]==0) { node dr; dr.x=xx,dr.y=yy,dr.step=step2; P.push(dr); b[xx][yy]=1; } } } } int min1=999999; for(int i=0; i<n; i++) for(int j=0; j<m; j++) { if(a[i][j]=='@') { if(c1[i][j]+c2[i][j]<min1) min1=c1[i][j]+c2[i][j]; } } printf("%d\n",min1); } return 0; }