题目还是都有的吧!老规矩,直接来:
#include<bits/stdc++.h>
using namespace std;
int di[8]={-2,-1,1,2,2,1,-1,-2};
int dj[8]={1,2,2,1,-1,-2,-2,-1};
int a[105][105],n,m;
void dg(int dep,int i,int j)
{
a[i][j]=dep;
if(dep==n*m)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
exit(0);
}
for(int k=0;k<=7;k++)
{
int ti=i+di[k];
int tj=j+dj[k];
if(ti>0&&ti<=n&&tj>0&&tj<=m&&a[ti][tj]==0) dg(dep+1,ti,tj);
}
a[i][j]=0;
}
int main()
{
cin>>n>>m;
dg(1,1,1);
return 0;
}