提交时间:2024-08-21 16:08:12
运行 ID: 180565
#include<bits/stdc++.h> using namespace std; int n; int vis[15][15]; int dx[] = {0, 1, -1, 1, -1, 2, -2, 2, -2}; int dy[] = {0, 2, 2, -2, -2, 1, 1, -1, -1}; bool flag = 0; void dfs(int x, int y,int num) { vis[x][y] = num; if (num == n * n) { flag = 1; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cout << vis[i][j] << " "; } cout << "\n"; } return; } for (int i = 1; i <= 8; ++i) { int cx = x + dx[i], cy = y + dy[i]; if (cx > n || cx < 1) continue; if (cy > n || cy < 1) continue; if (vis[cx][cy]) continue; dfs(cx, cy, num + 1); if (flag) return; } vis[x][y] = 0; } int main() { int x, y; cin >> n >> x >> y; dfs(x, y, 1); if (!flag) cout << -1; }