"Bzoj 3299" [USACO2011 open]corn Maze corn maze (shortest way)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.