#include <iostream>
using namespace std;
const int row[8] = {-2, -1, +1, +2, +2, +1, -1, -2};
const int col[8] = {+1, +2, +2, +1, -1, -2, -2, -1};
void try2Move(int count, int x, int y, int** board, int n);
void printAns(int** board, int n);
bool isInBoard(int x, int y, int n);
int main() {
int n;
cin >> n;
int** board = new int*[n]();
for(int i=0; i<n; i++)
board[i] = new int[n]();
int x, y;
cin >> x >> y;
board[x][y] = 1;
try2Move(2, x, y, board, n);
for(int i=0; i<n; i++)
delete[] board[i];
delete[] board;
return 0;
}
void printAns(int** board, int n) {
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cout << board[i][j] << " ";
}
cout<<"\n";
}
}
bool isInBoard(int x, int y, int n) {
if(x < 0 || x >= n) return false;
if(y < 0 || y >= n) return false;
return true;
}
//===============================================================
bool checkAns = false;
bool nextCellIsFirstCell(int x, int y, int** board, int n) {
for (int i=0; i<8; i++) {
int xN = x + row[i];
int yN = y + col[i];
if(!isInBoard(xN, yN, n)) continue;
if(board[xN][yN] == 1)
return true;
}
return false;
}
int numCan(pair<int, int> cur, int** board, int n) {
int x = cur.first;
int y = cur.second;
int num = 0;
for(int i=0; i<8; i++) {
int xN = x + row[i];
int yN = y + col[i];
if(!isInBoard(xN, yN, n)) continue;
if(board[xN][yN] > 0) continue;
num++;
}
return num;
}
void try2Move(int count, int x, int y, int** board, int n) {
if(checkAns == true) return;
if(count > n*n) {
if(!nextCellIsFirstCell(x, y, board, n)) return;
printAns(board, n);
checkAns = true;
}
pair<int, int> candidates[8];
int nCandidate = 0;
for (int i=0; i<8; i++) {
int xN = x + row[i];
int yN = y + col[i];
if(!isInBoard(xN, yN, n)) continue;
//If the number of candidates of first knight's
//position is 0, it means there're no way back
//to the first position
if(board[xN][yN] == 1) {
if(numCan({xN, yN}, board, n) == 0) return;
}
if(board[xN][yN] == 0) {
candidates[nCandidate++] = {xN, yN};
}
}
// Sort candidates by the number of next_candidates
// using bubble sort
for(int i=0; i < nCandidate - 1; i++) {
for (int j = 0; j < nCandidate - i - 1; j++) {
int num_a = numCan(candidates[j], board, n);
int num_b = numCan(candidates[j+1], board, n);
if(num_a > num_b)
swap(candidates[j], candidates[j+1]);
}
}
for(int i=0; i < nCandidate; i++) {
int xN = candidates[i].first;
int yN = candidates[i].second;
if(!isInBoard(xN, yN, n)) continue;
if(board[xN][yN] == 0) {
board[xN][yN] = count;
try2Move(count+1, xN, yN, board, n);
board[xN][yN] = 0;
}
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgogCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKY29uc3QgaW50IHJvd1s4XSA9IHstMiwgLTEsICsxLCArMiwgKzIsICsxLCAtMSwgLTJ9Owpjb25zdCBpbnQgY29sWzhdID0geysxLCArMiwgKzIsICsxLCAtMSwgLTIsIC0yLCAtMX07CiAKdm9pZCB0cnkyTW92ZShpbnQgY291bnQsIGludCB4LCBpbnQgeSwgaW50KiogYm9hcmQsIGludCBuKTsKdm9pZCBwcmludEFucyhpbnQqKiBib2FyZCwgaW50IG4pOwpib29sIGlzSW5Cb2FyZChpbnQgeCwgaW50IHksIGludCBuKTsKIAppbnQgbWFpbigpIHsKICAgIGludCBuOwogICAgY2luID4+IG47CiAKICAgIGludCoqIGJvYXJkID0gbmV3IGludCpbbl0oKTsKICAgIGZvcihpbnQgaT0wOyBpPG47IGkrKykKICAgICAgICBib2FyZFtpXSA9IG5ldyBpbnRbbl0oKTsKIAogICAgaW50IHgsIHk7CiAgICBjaW4gPj4geCA+PiB5OwogCiAgICBib2FyZFt4XVt5XSA9IDE7CiAgICB0cnkyTW92ZSgyLCB4LCB5LCBib2FyZCwgbik7CiAKICAgIGZvcihpbnQgaT0wOyBpPG47IGkrKykKICAgICAgICBkZWxldGVbXSBib2FyZFtpXTsKICAgIGRlbGV0ZVtdIGJvYXJkOwogCiAgICByZXR1cm4gMDsKfQogCnZvaWQgcHJpbnRBbnMoaW50KiogYm9hcmQsIGludCBuKSB7CiAgICBmb3IoaW50IGk9MDsgaTxuOyBpKyspIHsKICAgICAgICBmb3IoaW50IGo9MDsgajxuOyBqKyspIHsKICAgICAgICAgICAgY291dCA8PCBib2FyZFtpXVtqXSA8PCAiICI7CiAgICAgICAgfQogICAgICAgIGNvdXQ8PCJcbiI7CiAgICB9Cn0KIApib29sIGlzSW5Cb2FyZChpbnQgeCwgaW50IHksIGludCBuKSB7CiAgICBpZih4IDwgMCB8fCB4ID49IG4pIHJldHVybiBmYWxzZTsKICAgIGlmKHkgPCAwIHx8IHkgPj0gbikgcmV0dXJuIGZhbHNlOwogICAgcmV0dXJuIHRydWU7Cn0KIAovLz09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PQoKYm9vbCBjaGVja0FucyA9IGZhbHNlOwoKYm9vbCBuZXh0Q2VsbElzRmlyc3RDZWxsKGludCB4LCBpbnQgeSwgaW50KiogYm9hcmQsIGludCBuKSB7CiAgICBmb3IgKGludCBpPTA7IGk8ODsgaSsrKSB7CiAgICAgICAgaW50IHhOID0geCArIHJvd1tpXTsKICAgICAgICBpbnQgeU4gPSB5ICsgY29sW2ldOwogICAgICAgIGlmKCFpc0luQm9hcmQoeE4sIHlOLCBuKSkgY29udGludWU7CiAgICAgICAgCiAgICAgICAgaWYoYm9hcmRbeE5dW3lOXSA9PSAxKQogICAgICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIH0KICAgIHJldHVybiBmYWxzZTsKfQoKaW50IG51bUNhbihwYWlyPGludCwgaW50PiBjdXIsIGludCoqIGJvYXJkLCBpbnQgbikgewogICAgaW50IHggPSBjdXIuZmlyc3Q7CiAgICBpbnQgeSA9IGN1ci5zZWNvbmQ7CiAgICBpbnQgbnVtID0gMDsKICAgIAogICAgZm9yKGludCBpPTA7IGk8ODsgaSsrKSB7CiAgICAgICAgaW50IHhOID0geCArIHJvd1tpXTsKICAgICAgICBpbnQgeU4gPSB5ICsgY29sW2ldOwogICAgICAgIGlmKCFpc0luQm9hcmQoeE4sIHlOLCBuKSkgY29udGludWU7CiAgICAgICAgCiAgICAgICAgaWYoYm9hcmRbeE5dW3lOXSA+IDApIGNvbnRpbnVlOwogICAgICAgIG51bSsrOwogICAgfQogICAgCiAgICByZXR1cm4gbnVtOwp9Cgp2b2lkIHRyeTJNb3ZlKGludCBjb3VudCwgaW50IHgsIGludCB5LCBpbnQqKiBib2FyZCwgaW50IG4pIHsKICAgIGlmKGNoZWNrQW5zID09IHRydWUpIHJldHVybjsKICAgIGlmKGNvdW50ID4gbipuKSB7CiAgICAgICAgaWYoIW5leHRDZWxsSXNGaXJzdENlbGwoeCwgeSwgYm9hcmQsIG4pKSByZXR1cm47CiAgICAgICAgcHJpbnRBbnMoYm9hcmQsIG4pOwogICAgICAgIGNoZWNrQW5zID0gdHJ1ZTsKICAgIH0KICAgIAogICAgcGFpcjxpbnQsIGludD4gY2FuZGlkYXRlc1s4XTsKICAgIGludCBuQ2FuZGlkYXRlID0gMDsKICAgIAogICAgZm9yIChpbnQgaT0wOyBpPDg7IGkrKykgewogICAgICAgIGludCB4TiA9IHggKyByb3dbaV07CiAgICAgICAgaW50IHlOID0geSArIGNvbFtpXTsKICAgICAgICBpZighaXNJbkJvYXJkKHhOLCB5TiwgbikpIGNvbnRpbnVlOwogICAgICAgIAogICAgICAgIC8vSWYgdGhlIG51bWJlciBvZiBjYW5kaWRhdGVzIG9mIGZpcnN0IGtuaWdodCdzCiAgICAgICAgLy9wb3NpdGlvbiBpcyAwLCBpdCBtZWFucyB0aGVyZSdyZSBubyB3YXkgYmFjayAKICAgICAgICAvL3RvIHRoZSBmaXJzdCBwb3NpdGlvbgogICAgICAgIGlmKGJvYXJkW3hOXVt5Tl0gPT0gMSkgewogICAgICAgICAgICBpZihudW1DYW4oe3hOLCB5Tn0sIGJvYXJkLCBuKSA9PSAwKSByZXR1cm47CiAgICAgICAgfQogICAgICAgIAogICAgICAgIGlmKGJvYXJkW3hOXVt5Tl0gPT0gMCkgewogICAgICAgICAgICBjYW5k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