int a[10][10];
int n;
int count=0;
void solve(int i,int j,int k,bool& ok){
if(k>n*n){
count++;
ok=true;
cout<<"第"<<count<<"种情况:"<<endl;
for(i=0;i<n;i++){
for(j=0;j<n;j++)
cout<<setw(4)<<a[i][j];
cout<<endl;
}
int m;
cout<<"是否继续显示?1、继续 2、退出"<<endl;
cin>>m;
if(m==1) return;
else if(m==2) exit(0);
}
if(!a[i][j]&&i>=0&&i<=n-1&&j>=0&&j<=n-1){//从八个方向判断骑士是不是能够走
a[i][j]=k;
solve(i-2,j-1,k+1,ok);
if(ok){
a[i][j]=0;
ok=false;
return;
}
solve(i-2,j+1,k+1,ok);
if(ok){
a[i][j]=0;
ok=false;
return;
}
solve(i-1,j-2,k+1,ok);
if(ok){
a[i][j]=0;
ok=false;
return;
}
solve(i-1,j+2,k+1,ok);
if(ok){
a[i][j]=0;
ok=false;
原文请找腾讯752018766优,文-论'文.网http://www.youerw.com if(ok){
a[i][j]=0;
ok=false;
return;
}
solve(i+2,j-1,k+1,ok);
if(ok){
a[i][j]=0;
ok=false;
return;
}
solve(i+2,j+1,k+1,ok);
a[i][j]=0;
return;
}
else
return;
}
int main(){
int i,j;
cout<<"输入正方形棋盘的横纵坐标数:";
cin>>n;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=0;
int x,y;
cout<<"请输入出发位置的坐标(x,y): ";
cin>>x>>y;
bool ok=false;
solve(x-1,y-1,1,ok);
if(!count)
cout<<"情况个数:"<<count<<endl;
else
cout<<"该题无解!"<<endl;
return 0;