Find A-to
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4854 Accepted Submission (s): 1650
Problem Descriptionpass a year learning in Hangzhou, Yifenfei arrival hometown Ningbo at finally. Leave Ningbo One year, Yifenfei has many people to meet. Especially a good friend Merceki.
Yifenfei's home is on the countryside, but Merceki's home is in the center of the city. So Yifenfei made arrangements and Merceki to meet at a KFC. There is many KFC in Ningbo, they want to choose one and the total time to it is most smallest.
Now give your a Ningbo map, Both Yifenfei and Merceki can move up, down, left, right to the adjacent road by cost one minute S.
Inputthe input contains multiple test cases.
Each test case include, first, 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
Outputfor each test case output, the minimum total time, both Yifenfei and Merceki to arrival one of the KFC. Sure there is always has a KFC that can let them meet.
Sample Input
4 4y.#@.....#. @.. M4 4y.#@.....#. @#. M5 5[email protected].#....#...@. m.#...#
Sample Output
668866#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < queue>using namespace Std;const int n=220;const int inf=0xfffffff;int n,m;int ytime[n][n],mtime[n][n];int Map[N][N], Num[n][n],vis[n][n];int temp;void BFS (int x,int y) {int xx,yy;queue<int>q;xx=x;yy=y;vis[xx][yy]=1;num[xx][yy]= 1;//records the total number of steps Q.push (XX); Q.push (yy); while (!q.empty ()) {Xx=q.front (); Q.pop (); Yy=q.front (); Q.pop (); if (Map[xx][yy] ==1) {if (temp==1) {ytime[xx][yy]=num[xx][yy]-1;//is important here}else{mtime[xx][yy]=num[xx][yy]-1;}} int A, b;a=xx-1;b=yy; Discuss if (a>=1&&!vis[a][b]&&map[a][b]!=-1) {Num[a][b]=num[xx][yy]+1;vis[a][b]=1;q.push (a) in four different cases ); Q.push (b);} A=xx;b=yy-1;if (b>=1&&!vis[a][b]&&map[a][b]!=-1) {Num[a][b]=num[xx][yy]+1;vis[a][b]=1;q.push ( a); Q.push (b);} A=xx+1;b=yy;if (a<=n&&!vis[a][b]&&map[a][b]!=-1) {Num[a][b]=num[xx][yy]+1;vis[a][b]=1;q.push ( a); Q.push (b);} A=xx;b=yy+1;if (b<=m&&!vis[a][b]&&map[a][b]!=-1) {Num[a][b]=num[xx][yy]+1;vis[a][b]=1;q.push (a); Q.push (b);}}} int main () {int I,j,yx,yy,mx,my;char ch;while (~scanf ("%d%d", &n,&m)) {memset (map,0,sizeof (map)); for (i=1;i <=n;i++) {for (j=1;j<=m;j++) {cin>>ch;if (ch== ' Y ') {yx=i,yy=j;} else if (ch== ' m ') {mx=i;my=j;} else if (ch== ' # ') {map[i][j]=-1;} else if (ch== ' @ ') {map[i][j]=1;}}} memset (ytime,0,sizeof (ytime)); memset (mtime,0,sizeof (Mtime)); memset (vis,0,sizeof (Vis)); memset (num,0,sizeof (num)); temp=1; Marks the difference between Y and M BFS (YX,YY); memset (vis,0,sizeof (Vis)); memset (num,0,sizeof (num)); temp=0; BFS (mx,my); int min=inf;for (i=1;i<=n;i++) {for (j=1;j<=m;j++) {if (ytime[i][j]&&mtime[i][j]&& ( YTIME[I][J]+MTIME[I][J]) <min) {min=ytime[i][j]+mtime[i][j];}} printf ("%d\n", min*11);} return 0;}
Hdoj-2612-find a way-two-way BFS