#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
int n, res[11][11]; // ma trận lưu lịch sử di chuyển của quân mã
bool f = false; // chưa tìm thấy cách đi
// kiểm tra tọa độ x y có nằm trong bàn cờ và chưa được thăm hay ko
bool ok(int x, int y) {
return (x >= 0 && y >= 0 && x < n && y < n && res[x][y] == -1);
}
// di chuyển sang x y vào bước thứ d
void solve(int x, int y, int d);
// dò các ô xung quanh điểm x y trong bước thứ d
void visit(int x, int y, int d) {
if (d == n * n) {
f = true;
return;
}
const int moves[8][2] = {
{1, 2}, {1, -2}, {-1, 2}, {-1, -2},
{2, 1}, {2, -1}, {-2, 1}, {-2, -1}
};
for (const auto& move : moves) {
int nx = x + move[0];
int ny = y + move[1];
solve(nx, ny, d);
}
}
void solve(int x, int y, int d) {
if (f)
return;
if (ok(x, y)) {
res[x][y] = d + 1;
visit(x, y, d + 1);
if (!f)
res[x][y] = -1; // Nếu tour không thành công, bỏ đánh dấu
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res[i][j] = -1;
res[0][0] = 1;
visit(0, 0, 1);
if (f) {
// Tính toán độ dài tối đa của số trong ma trận
int max_width = to_string(n * n).length();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << setw(max_width) << res[i][j] << " ";
cout << endl;
}
} else {
cout << "Hong thayy" << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8aW9tYW5pcD4gCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG4sIHJlc1sxMV1bMTFdOyAvLyBtYSB0cuG6rW4gbMawdSBs4buLY2ggc+G7rSBkaSBjaHV54buDbiBj4bunYSBxdcOibiBtw6MKYm9vbCBmID0gZmFsc2U7IC8vIGNoxrBhIHTDrG0gdGjhuqV5IGPDoWNoIMSRaSAKCgovLyBraeG7g20gdHJhIHThu41hIMSR4buZIHggeSBjw7MgbuG6sW0gdHJvbmcgYsOgbiBj4budIHbDoCBjaMawYSDEkcaw4bujYyB0aMSDbSBoYXkga28gCmJvb2wgb2soaW50IHgsIGludCB5KSB7CiAgICByZXR1cm4gKHggPj0gMCAmJiB5ID49IDAgJiYgeCA8IG4gJiYgeSA8IG4gJiYgcmVzW3hdW3ldID09IC0xKTsKfQoKCi8vIGRpIGNodXnhu4NuIHNhbmcgeCB5IHbDoG8gYsaw4bubYyB0aOG7qSBkCnZvaWQgc29sdmUoaW50IHgsIGludCB5LCBpbnQgZCk7CgoKLy8gZMOyIGPDoWMgw7QgeHVuZyBxdWFuaCDEkWnhu4NtIHggeSB0cm9uZyBixrDhu5tjIHRo4bupIGQKdm9pZCB2aXNpdChpbnQgeCwgaW50IHksIGludCBkKSB7CiAgICBpZiAoZCA9PSBuICogbikgewogICAgICAgIGYgPSB0cnVlOwogICAgICAgIHJldHVybjsKICAgIH0KCiAgICBjb25zdCBpbnQgbW92ZXNbOF1bMl0gPSB7CiAgICAgICAgezEsIDJ9LCB7MSwgLTJ9LCB7LTEsIDJ9LCB7LTEsIC0yfSwKICAgICAgICB7MiwgMX0sIHsyLCAtMX0sIHstMiwgMX0sIHstMiwgLTF9CiAgICB9OwoKICAgIGZvciAoY29uc3QgYXV0byYgbW92ZSA6IG1vdmVzKSB7CiAgICAgICAgaW50IG54ID0geCArIG1vdmVbMF07CiAgICAgICAgaW50IG55ID0geSArIG1vdmVbMV07CiAgICAgICAgc29sdmUobngsIG55LCBkKTsKICAgIH0KfQoKdm9pZCBzb2x2ZShpbnQgeCwgaW50IHksIGludCBkKSB7CiAgICBpZiAoZikKICAgICAgICByZXR1cm47CiAgICBpZiAob2soeCwgeSkpIHsKICAgICAgICByZXNbeF1beV0gPSBkICsgMTsKICAgICAgICB2aXNpdCh4LCB5LCBkICsgMSk7CiAgICAgICAgaWYgKCFmKQogICAgICAgICAgICByZXNbeF1beV0gPSAtMTsgIC8vIE7hur91IHRvdXIga2jDtG5nIHRow6BuaCBjw7RuZywgYuG7jyDEkcOhbmggZOG6pXUKICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICBjaW4gPj4gbjsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgbjsgaisrKQogICAgICAgICAgICByZXNbaV1bal0gPSAtMTsKICAgIHJlc1swXVswXSA9IDE7CiAgICB2aXNpdCgwLCAwLCAxKTsKCiAgICBpZiAoZikgewogICAgICAgIC8vIFTDrW5oIHRvw6FuIMSR4buZIGTDoGkgdOG7kWkgxJFhIGPhu6dhIHPhu5EgdHJvbmcgbWEgdHLhuq1uCiAgICAgICAgaW50IG1heF93aWR0aCA9IHRvX3N0cmluZyhuICogbikubGVuZ3RoKCk7CgogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgbjsgaisrKQogICAgICAgICAgICAgICAgY291dCA8PCBzZXR3KG1heF93aWR0aCkgPDwgcmVzW2ldW2pdIDw8ICIgIjsKICAgICAgICAgICAgY291dCA8PCBlbmRsOwogICAgICAgIH0KICAgIH0gZWxzZSB7CiAgICAgICAgY291dCA8PCAiSG9uZyB0aGF5eSIgPDwgZW5kbDsKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=