Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6678 Accepted Submission(s): 2220
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.
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
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
66 8866
#include<cstdio> #include<iostream> #include<queue> using namespace std; int dir[4][2]={1,0,-1,0,0,1,0,-1}; struct node { int xx=0; int yy=0; }; int d1[202][202]={0}; int d2[202][202]={0}; char map[202][202]; int main() { int n=0,m=0; int x1=0,y1=0,x2=0,y2=0; while(scanf("%d%d",&n,&m)!=EOF) { int min=9999; getchar(); memset(map,0,sizeof(map)); memset(d1,0,sizeof(d1)); memset(d2,0,sizeof(d2)); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='Y') { x1=i; y1=j; } else if(map[i][j]=='M') { x2=i; y2=j; } } getchar(); } d1[x1][y1]=1; queue<node> q; node p; p.xx=x1; p.yy=y1; q.push(p); while(q.size()) { node p1=q.front(); q.pop(); for(int i=0;i<4;i++) { int dx=p1.xx+dir[i][0]; int dy=p1.yy+dir[i][1]; if(dx<=n&&dx>=1&&dy<=m&&dy>=1&&map[dx][dy]!='#'&&d1[dx][dy]==0) { d1[dx][dy]=d1[p1.xx][p1.yy]+1; node p2; p2.xx=dx; p2.yy=dy; q.push(p2); } } } d2[x2][y2]=1; p.xx=x2; p.yy=y2; q.push(p); while(q.size()) { node p1=q.front(); q.pop(); for(int i=0;i<4;i++) { int dx=p1.xx+dir[i][0]; int dy=p1.yy+dir[i][1]; if(dx<=n&&dx>=1&&dy<=m&&dy>=1&&map[dx][dy]!='#'&&d2[dx][dy]==0) { d2[dx][dy]=d2[p1.xx][p1.yy]+1; if(map[dx][dy]=='@') { if(min>d1[dx][dy]+d2[dx][dy]) { min=d1[dx][dy]+d2[dx][dy]; } } node p2; p2.xx=dx; p2.yy=dy; q.push(p2); } } } printf("%d\n",(min-2)*11); } return 0; }