Main topic
Give you a map of nn*mm rectangles. Some of the above points have enemy camps. Give you the beginning and the end, you find an optimal path. The shortest distance from the enemy battalion that satisfies the point on the optimal path is the shortest of all paths. If there is more than one path to find the shortest.
Analysis
The shortest distance from the enemy battalion is determined by two points. Then BFS to verify.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cmath>6#include <string>7#include <queue>8#include <stack>9#include <map>Ten#include <vector> One #defineMAXN 1010 A #defineMAXN 2005 - #defineMAXM 20000005 - #defineINF 100000000000000 the #defineOO 1000000007 - using namespacestd; - -typedefLong LongLL; + struct Point - { + intx,y,dist; A}p[maxn*MAXN]; at intMid,n,nn,mm,stx,sty,edx,edy; - intVIST[MAXN][MAXN],VISTDIST[MAXN][MAXN]; - intdirx[]={0,-1,0,1}; - intdiry[]={-1,0,1,0}; - intMax_dist,dist1,dist2; - voidBFS1 () in { -Queue<point>Q; to while(!q.empty ()) + Q.pop (); - for(intI=0; i<n;i++) the Q.push (P[i]); * while(!q.empty ()) $ {Panax NotoginsengPoint tem=Q.front (); - Q.pop (); the + for(intI=0;i<4; i++) A { the intnewx=tem.x+Dirx[i]; + intnewy=tem.y+Diry[i]; - if(newx>=0&&newx<mm&&newy>=0&&newy<nn&&!Vist[newx][newy]) $ { $vist[newx][newy]=1; - intnewdist=tem.dist+1; -vistdist[newx][newy]=newdist; themax_dist=Max (max_dist,newdist); - Point pp;Wuyipp.x=newx; thepp.y=Newy; -pp.dist=newdist; Wu Q.push (PP); - } About } $ } - } - intBFS2 () - { AQueue<point>Q; + while(!q.empty ()) the Q.pop (); - Point pp; $pp.x=STX; thepp.y=sty; thepp.dist=0; the Q.push (PP); thememset (Vist,0,sizeof(vist)); - if(vistdist[stx][sty]<mid) in return 0; the the while(!q.empty ()) About { thePoint tem=Q.front (); the Q.pop (); the if(tem.x==edx&&tem.y==Edy) + { -dist1=mid; theDist2=tem.dist;Bayi return 1; the } the for(intI=0;i<4; i++) - { - intxx=tem.x+Dirx[i]; the intyy=tem.y+Diry[i]; the if(xx>=0&&xx<mm&&yy>=0&&yy<nn&&!vist[xx][yy]&&vistdist[xx][yy]>=mid) the { thevist[xx][yy]=1; - Point pp; thepp.x=xx; thepp.y=yy; thepp.dist=tem.dist+1;94 Q.push (PP); the } the the }98 } About return 0; - 101 }102 intMain ()103 {104 intT; thescanf"%d",&t);106 while(t--)107 {108scanf" %d%d%d",&n,&mm,&nn);109scanf"%d %d%d%d",&stx,&sty,&edx,&edy); thememset (Vist,0,sizeof(vist));111 for(intI=0; i<n;i++) the {113scanf"%d%d",&p[i].x,&p[i].y); thep[i].dist=0; thevistdist[p[i].x][p[i].y]=0; thevist[p[i].x][p[i].y]=1;117 }118max_dist=-1;119 BFS1 (); - intL=0, r=max_dist;121 while(l<=R)122 {123Mid= (L+R)/2;124 if(BFS2 ()) theL=mid+1;126 Else127r=mid-1; - 129 } theprintf"%d%d\n", dist1,dist2);131 } the return 0;133}
Note that the number of queues is small, and that is what should be written when you insert a queue.
Point pp; pp.x=newx; Pp.y=newy; Pp.dist=newdist; Q.push (PP);
If this is written, it will be CE
Q.push (point) {newx,newy,newdist});
Hdu 2337 Escape from Enemy Territory