http://poj.org/problem?id=2243
最简单的bfs输出下路径
#include<stdio.h> #include<string.h> struct { int x,y,step; } queue[10000],e; bool mark[10][10]; int dir[8][2]= {2,1,1,2,-1,2,-2,1,-2,-1,-1,-2,1,-2,2,-1},x1,y1,x2,y2,i;; char y11,y22; int bfs() { int start,tail,x,y,step; start=tail=0; e.x=x1; e.y=y1; e.step=0; queue[start]=e; while(start<=tail) { x=queue[start].x; y=queue[start].y; step=queue[start].step; start++; for(i=0;i<8;i++) { e.x=dir[i][0]+x; e.y=dir[i][1]+y; e.step=step+1; if(e.x<=0||e.y<=0||e.x>8||e.y>8||mark[e.x][e.y]) continue; if((e.x==x2)&&(e.y==y2)) { printf("To get from %c%d to %c%d takes %d knight moves.\n",y11,x1,y22,x2,e.step); return 0; } ++tail; queue[tail]=e; mark[e.x][e.y]=true; } } } int main(void) { while(scanf("%c%d %c%d",&y11,&x1,&y22,&x2)!=EOF) { getchar(); y1=y11-'a'+1; y2=y22-'a'+1; if((x1==x2)&&(y1==y2)) { printf("To get from %c%d to %c%d takes 0 knight moves.\n",y11,x1,y22,x2); continue; } memset(mark,false,sizeof(mark)); bfs(); } return 0; }