请用回溯法解决骑士巡游问题(骑士巡游问题是指:在8*8的国家象棋棋盘上,一匹马从任意位置出发,按照象棋的规则走,要求踏遍整个棋盘,并且每个格子只能经过一次)。 #include<iostream> #include<cstring> #define N 8 using namespace std; bool v[N][N]; int a[N][N],sx,sy,d[8][2]={{-2,-1},{-1,-2},{1,-2},{2,-1},{-2,1},{-1,2},{1,2},{2,1}}; void print(){ for(int i=0;i<N;i++){ for(int j=0;j<N;j++) cout<<a[i][j]<<" "; cout<<endl; } cout<<endl; } bool f(int sx,int sy,int step){ if(step==N*N){ print(); return true; } for(int i=0;i<8;i++){ int x=sx+d[i][0],y=sy+d[i][1]; if(0<=x&&x<N&&0<=y&&y<N&&!v[x][y]){ a[x][y]=step; v[x][y]=true; if(f(x,y,step+1)) return true; v[x][y]=false; } } } int main(){ while(cin>>sx>>sy){ memset(v,false,sizeof(v)); a[sx][sy]=0; v[sx][sy]=true; f(sx,sy,1); } return 0; } 当输入 0 0 时,算了两个小时都没出结果,求高效算法。