考查:BFS 提交情况:一次AC 时间:313MS 内存:1672K 代码: #include <stdio.h> typedef struct _n { int n; int step; }node; typedef struct _p { int row; int col; int pre; }path; node queue[65536]; int visit[65536]; path paths[65536]; int num; int map[16]= { 0x111f,0x222f,0x444f,0x888f, 0x11f1,0x22f2,0x44f4,0x88f8, 0x1f11,0x2f22,0x4f44,0x8f88, 0xf111,0xf222,0xf444,0xf888 }; void print(int idx) { if(paths[idx].pre<0) return; print(paths[idx].pre); printf("%d %d/n",paths[idx].row,paths[idx].col); } void bfs() { int i,j,front,back; node s,t; path p; front=back=0; s.n=num; s.step=0; p.row=p.col=0; p.pre=-1; visit[s.n]=1; queue[back++]=s; paths[s.n]=p; while(front!=back) { s=queue[front++]; s.step++; for(i=0;i<4;i++) { for(j=0;j<4;j++) { t=s; t.n^=map[i*4+j]; if(t.n==0xffff) { p.row=i+1; p.col=j+1; p.pre=s.n; paths[t.n]=p; printf("%d/n",s.step); print(t.n); return; } if(!visit[t.n]) { visit[t.n]=1; p.row=i+1; p.col=j+1; p.pre=s.n; queue[back++]=t; paths[t.n]=p; } } } } } int main() { int i,j; char c; num=0; for(i=0;i<4;i++) { for(j=0;j<4;j++) { scanf("%c",&c); if(c=='-') num+=(1<<(i*4+j)); } getchar(); } if(num==0xffff) printf("0/n"); else bfs(); return 0; }