关键词:
递归、回溯、搜索
#include<cstdio>
int n;
int a[8]= {-2,-1,1,2,2,1,-1,-2};
int b[8]= {-1,-2,-2,-1,1,2,2,1};
int count=0;
int c[100][100]= {0};
void dfs(int x,int y,int t) {
if(t==n*n) {
count++;
printf("\n%d\n",count);
for(int i=0; i<n; ++i) {
for(int j=0; j<n; ++j)
printf("%d ",c[i][j]);
printf("\n");
}
return ;
}
for(int i=0; i<8; ++i) {
int x1=x+a[i];
int y1=y+b[i];
if(x1>=0&&y1>=0&&x1<n&&y1<n&&c[x1][y1]==0) {//判断边界
c[x1][y1]=t+1;/开始递归、回溯
dfs(x1,y1,t+1);
c[x1][y1]=0;
}
}
}
int main() {
c[0][0]=1;
scanf("%d",&n);
dfs(0,0,1);
printf("%d\n",count);
return 0;
}