Topic: Given a bad point of the grid diagram, from point A to point B, the need for a minimum turning
Naked SPFA ... It's good to record the direction in the state.
Water and water more healthy ~ ~
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 10100# Define P (x, y) ((x) *n-n+ (y)) using namespace std;const int dx[]={0,0,1,-1};const int dy[]={1,-1,0,0};int n,a,b;int map[m], F[m<<2];void SPFA () {static int q[65540];static bool V[m<<2];static unsigned short r,h;int i;memset (f,0x3f, sizeof f); for (i=0;i<4;i++) f[q[++r]=a<<2|i]=0,v[a<<2|i]=true;while (r!=h) {int x=q[++h];v[x]=false; int _x= ((x>>2)-1)/n+1,_y= ((x>>2)-1)%n+1,_dir=x&3;for (i=0;i<4;i++) {int xx=_x+dx[i],yy=_y+dy[i] ; if (xx<=0| | yy<=0| | xx>n| | Yy>n) continue;if (Map[p (xx,yy)) Continue;int y=p (xx,yy) <<2|i;if (f[y]>f[x]+ (I!=_dir)) {f[y]=f[x]+ (i!=_ DIR); if (!v[y]) v[y]=true,q[++r]=y;}}}} int main () {int i,j;cin>>n;for (i=1;i<=n;i++) {static char s[m];scanf ("%s", s+1); for (j=1;j<=n;j++) switch (s [j]) {case ' x ': Map[p (i,j)]=1;break;case ' A ': a=p (i,j); Break;case ' B ': b=p (I,J); SPFA (); Cout<<min (min (f[b<<2|0],f[b<<2|1]), Min (f[b<<2|2],f[b<<2|3])) <<endl;return 0;}
Bzoj 1644 Usaco2007 Oct Obstacle Course Obstacle training course SPFA