提交时间:2024-08-21 16:09:01
运行 ID: 180567
#include<iostream> using namespace std; int a[15][15],n,sum; int dx[8]={-1,-2,-2,-1,2,2,1,1}; int dy[8]={2,1,-1,-2,1,-1,2,-2}; int flag=0; void bits(int x,int y,int num){ int x1=x,y1=y; if(num==n*n){ flag=1; return; } else{ for(int i=0;i<8;i=i+1){ x1=x+dx[i]; y1=y+dy[i]; if(x1>0 && x1<=n && y1>0 && y1<=n && !a[x1][y1]){ num=num+1; a[x1][y1]=num; bits(x1,y1,num); num=num-1; if(flag){ return; } a[x1][y1]=0; } x1=x-dx[i]; y1=y-dy[i]; } } } int main(){ int x,y; cin>>n>>x>>y; a[x][y]=1; bits(x,y,1); for(int i=1;i<=n;i=i+1){ for(int j=1;j<=n;j=j+1){ cout<<a[i][j]<<' '; } cout<<'\n'; } return 0; }