Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
吐槽,同学写的普通BFS和我的双向bfs一样快!不爽!贴我写萎了的代码#include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #include <algorithm> #include <iostream> using namespace std; struct MT { bool map[5][5]; }S,T,ZT; struct Q { int z,t,fg; }qt,sta; struct LAST { int ax,ay,bx,by,lt; }last[1<<17],stk[100],zz; priority_queue<Q> q; int dx[2]={0,1}; int dy[2]={1,0}; int anti[3],dis[1<<17],vis[1<<17],ans,pre,suf; inline bool operator <(const Q &a,const Q &b) { return a.t>b.t; } inline int close(const MT &a) { int tmp=0; for(int i=1;i<=4;i++) for(int j=1;j<=4;j++) if(a.map[i][j]) tmp|=(1<<((i-1)*4+j-1)); return tmp; } inline void open(int a) { for(int i=1;i<=4;i++) for(int j=1;j<=4;j++) { ZT.map[i][j]=a&1; a>>=1; } } bool read() { memset(last,-1,sizeof last); char str[10]; for(int i=1;i<=4;i++) { scanf("%s",str+1); for(int j=1;j<=4;j++) if(str[j]=='1') S.map[i][j]=1; } qt.z=close(S); qt.t=0; qt.fg=1; q.push(qt); vis[qt.z]=1; dis[qt.z]=0; last[qt.z].lt=-1; getchar(); for(int i=1;i<=4;i++) { scanf("%s",str+1); for(int j=1;j<=4;j++) if(str[j]=='1') T.map[i][j]=1; } qt.z=close(T); qt.t=0; qt.fg=2; q.push(qt); vis[qt.z]=2; dis[qt.z]=0; last[qt.z].lt=-1; if(qt.z==close(S))//不需要移动 { puts("0"); return false; } else return true; } void go() { anti[0]=-1;anti[1]=2;anti[2]=1; while(!q.empty()) { sta=q.top(); q.pop(); open(sta.z); for(int i=1,nx,ny;i<=4;i++) for(int j=1;j<=4;j++) for(int k=0;k<2;k++) { nx=i+dx[k]; ny=j+dy[k]; if(nx>4||ny>4) continue; swap(ZT.map[i][j],ZT.map[nx][ny]); qt.z=close(ZT); swap(ZT.map[i][j],ZT.map[nx][ny]); if(vis[qt.z]==0)//未拓展的节点 { vis[qt.z]=sta.fg; dis[qt.z]=sta.t+1; qt.t=dis[qt.z]; qt.fg=sta.fg; q.push(qt); last[qt.z].lt=sta.z; last[qt.z].ax=i; last[qt.z].ay=j; last[qt.z].bx=nx; last[qt.z].by=ny; } else if(vis[qt.z]==anti[sta.fg])//另外一端拓展出的点 { if(sta.fg==1) {pre=sta.z; suf=qt.z;} else {pre=qt.z; suf=sta.z;} zz.ax=i;zz.ay=j; zz.bx=nx; zz.by=ny; ans=dis[qt.z]+sta.t+1; return; } } } } void prt() { printf("%d\n",ans); int p=0; while(last[pre].lt!=-1) { stk[++p]=last[pre]; pre=last[pre].lt; } for(int i=p;i>=1;i--) printf("%d %d %d %d\n",stk[i].ax,stk[i].ay,stk[i].bx,stk[i].by); printf("%d %d %d %d\n",zz.ax,zz.ay,zz.bx,zz.by); while(last[suf].lt!=-1) { printf("%d %d %d %d\n",last[suf].ax,last[suf].ay,last[suf].bx,last[suf].by); suf=last[suf].lt; } } int main() { if(read()) { go(); prt(); } system("pause"); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator