One is the shortest, and the edge length is 1, so each point is searched only once.
1 /**************************************************************2 problem:32993 User:mt_chan4 language:c++5 result:accepted6 time:72 Ms7 memory:2420 KB8 ****************************************************************/9 Ten#include <cstdio> One#include <cstdlib> A#include <cstring> -#include <iostream> -#include <algorithm> the#include <queue> - using namespacestd; - #defineMAXN 310 - #defineINF 0XFFFFFFF + - intkx[ -]; + inta[maxn][maxn],tr[maxn*MAXN]; A CharS[MAXN]; at - intdis[maxn*maxn],st,ed; - intn,m; - -queue<int>Q; - voidSPFA () in { - while(!q.empty ()) Q.pop (); tomemset (dis,-1,sizeof(DIS)); +Q.push (ST);d is[st]=0; - while(!q.empty ()) the { * intx=Q.front (); $ intnx= (x1)/m+1, ny=x-(nx-1)*m;Panax Notoginseng if(ny>1&&tr[x-1]!=-1&&dis[tr[x-1]]==-1) - { thedis[tr[x-1]]=dis[x]+1; + if(tr[x-1]==ed) Break; AQ.push (tr[x-1]); the } + if(ny<m&&tr[x+1]!=-1&&dis[tr[x+1]]==-1) - { $dis[tr[x+1]]=dis[x]+1; $ if(tr[x+1]==ed) Break; -Q.push (tr[x+1]); - } the if(nx>1&&tr[x-m]!=-1&&dis[tr[x-m]]==-1) - {Wuyidis[tr[x-m]]=dis[x]+1; the if(tr[x-m]==ed) Break; -Q.push (tr[x-m]); Wu } - if(nx<n&&tr[x+m]!=-1&&dis[tr[x+m]]==-1) About { $dis[tr[x+m]]=dis[x]+1; - if(tr[x+m]==ed) Break; -Q.push (tr[x+m]); - } A Q.pop (); + } theprintf"%d\n", dis[ed]); - } $ the intMain () the { thescanf"%d%d",&n,&m); thememset (kx,-1,sizeof(KX)); -memset (TR,0,sizeof(tr)); in for(intI=1; i<=n;i++) the { thescanf"%s", s); About for(intj=0; j<m;j++) the { the intnow= (I-1) *m+j+1; the if(s[j]=='@') st=Now ; + Else if(s[j]=='=') ed=Now ; - Else if(s[j]=='#') tr[now]=-1; the Else if(s[j]>='A'&&s[j]<='Z')Bayi { the intkk=s[j]-'A'+1; the if(kx[kk]==-1) kx[kk]=Now ; - Elsetr[now]=kx[kk],tr[kx[kk]]=Now ; - } the } the } the for(intI=1; i<=n*m;i++)if(tr[i]==0) tr[i]=i; the SPFA (); - return 0; the}
View Code
"Bzoj 3299" [USACO2011 open]corn Maze corn maze (shortest way)