Supposedly a very simple bfs.
I was just beginning to write a two-way BFS, and the results never went away.
And then I thought, if the search speed is different, there will be a bug.
I had to change it into two separate BFS + last enumeration to determine the minimum value
1#include <iostream>2#include <queue>3#include <cstdio>4#include <cstring>5 using namespacestd;6 Const intd[4][2]={1,0,-1,0,0,1,0,-1};7 structnode{8 intt,x,y;9 }s,e;Ten intvis1[205][205],vis2[205][205]; One Charmap[205][205]; A intn,m; -Queue <node>QF,QB; - BOOLCheck (node&p) the { - returnp.x>=0&&p.x<n&&p.y>=0&&p.y<m&&map[p.x][p.y]!='#'; - } - voidBFS () + { - inti; + node now,nxt; Amemset (Vis1,0,sizeof(VIS1)); atmemset (Vis2,0,sizeof(VIS2)); - while(!qf.empty ()) Qf.pop (); - while(!qb.empty ()) Qb.pop (); - Qf.push (s); Qb.push (e); - while(!qf.empty ()) - { innow=Qf.front (); Qf.pop (); - for(i=0;i<4;++i) to { +nxt.x=now.x+d[i][0]; -nxt.y=now.y+d[i][1]; thenxt.t=now.t+1; * if(Check (NXT) &&!Vis1[nxt.x][nxt.y]) $ {Panax Notoginsengvis1[nxt.x][nxt.y]=nxt.t; - Qf.push (NXT); the } + } A } the while(!qb.empty ()) + { -now=Qb.front (); Qb.pop (); $ for(i=0;i<4;++i) $ { -nxt.x=now.x+d[i][0]; -nxt.y=now.y+d[i][1]; thenxt.t=now.t+1; - if(Check (NXT) &&!Vis2[nxt.x][nxt.y])Wuyi { thevis2[nxt.x][nxt.y]=nxt.t; - Qb.push (NXT); Wu } - } About } $ } - intMain () - { - inti,j; A while(~SCANF ("%d%d",&n,&m)) + { the for(i=0; i<n;i++) - { $scanf"%s", Map[i]); the for(j=0; j<m;j++) the { the if(map[i][j]=='Y') s.x=i,s.y=j,map[i][j]='#'; the if(map[i][j]=='M') e.x=i,e.y=j,map[i][j]='#'; - } in } thes.t=e.t=0; the BFS (); About intans=99999999; the for(i=0; i<n;i++) the for(j=0; j<m;j++) the if(map[i][j]=='@'&&vis1[i][j]&&Vis2[i][j]) + { -Ans=min (ans,vis1[i][j]+Vis2[i][j]); the }Bayi theprintf"%d\n", ans* One); the } - } - /* the 2 4 the [Email protected]# the M ... the */
HDU 2612-find A