#include #define ff first #define pb push_back #define ss second #define mpa make_pair using namespace std; typedef long long LL; int delta[2][8]; bool moveIsValid(int x, int y, int N); int travelAndFind(pair point1, pair point2, int N) { queue, int> > pairQueue; map, bool> map; pairQueue.push(mpa(point1, 0)); while(!pairQueue.empty()) { pair, int> temp = pairQueue.front(); pairQueue.pop(); if(temp.ff.ff == point2.ff && temp.ff.ss == point2.ss) return temp.ss; int x = temp.ff.ff; int y = temp.ff.ss; int dis = temp.ss + 1; if(map.count(mpa(x, y))) continue; map[mpa(x, y)] = true; for(int i = 0; i < 8; ++i) { int x1 = x + delta[0][i]; int y1 = y + delta[1][i]; if(moveIsValid(x1, y1, N)) pairQueue.push(mpa(mpa(x1, y1), dis)); } } return -1; } int calculateTravel(int N, int a1, int b1, int a2, int b2) { pair point1; point1.ff = a1; point1.ss = b1; pair point2; point2.ff = a2; point2.ss = b2; pair boundry; boundry.ff = N; boundry.ss = N; return travelAndFind(point1, point2, N); } bool moveIsValid(int x, int y, int N) { if(x <= 0 || y <= 0 || x > N || y > N) return false; return true; } void genSeq(int i, int j){ delta[0][0] = i; delta[0][1] = i; delta[0][2] = -i; delta[0][3] = -i; delta[0][4] = j; delta[0][5] = j; delta[0][6] = -j; delta[0][7] = -j; delta[1][0] = -j; delta[1][1] = j; delta[1][2] = j; delta[1][3] = -j; delta[1][4] = i; delta[1][5] = -i; delta[1][6] = i; delta[1][7] = -i; // del[0] = i; // del[1] = i; // del[2] = -i; // del[3] = -i; // del[4] = j; // del[5] = j; // del[6] = -j; // del[7] = -j; // del[0] = -j; // del[1] = j; // del[2] = j; // del[3] = -j; // del[4] = i; // del[5] = -i; // del[6] = i; // del[7] = -i; } int main() { int n; cin >> n; for (int i = 1; i < n; ++i) { for (int j = 1; j < n; ++j) { genSeq(i, j); printf("%d ", calculateTravel(n, 1, 1, n, n)); } printf("\n"); } return 0; }