提交时间:2024-08-21 15:42:28
运行 ID: 180532
#include<iostream> using namespace std; int n; int a[20][20]; int dx[] = {-2 , -2 , -1 , 1 , 2 , 2 , 1 , -1}; int dy[] = {-1 , 1 , 2 , 2 , 1 , -1 , -2 , -2}; int flag = 0; void dfs(int x , int y , int num) { int x1 , y1; if(num == n * n) { flag = 1; return; } for(int i = 0;i < 8;i++) { x1 = x + dx[i]; y1 = y + dy[i]; if(x1 > 0 && x1 <= n && y1 > 0 && y1 <= n && !a[x1][y1]) { num++; a[x1][y1] = num; dfs(x1 , y1 , num); num--; 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; dfs(x , y , 1); for(int i = 1;i <= n;i++) { for(int j = 1;j <= n;j++) { cout << a[i][j] << ' '; } cout << '\n'; } return 0; }