#include #include #include #include #include #include #include #include using namespace std; map, int> mp; struct State { int x; int y; int cost; State(int x1, int y1, int cost1) { x = x1; y = y1; cost = cost1; } }; bool inbound(int i, int j, int n) { return i >= 0 && j >= 0 && i < n && j < n; } int knight (int i, int j, int n) { pair input1 = make_pair(i,j); pair input2 = make_pair(j,i); auto ptr = mp.find(input1); if (ptr != mp.end()) return ptr->second; ptr = mp.find(input2); if (ptr != mp.end()) return ptr->second; int x = 0, y = 0; vector> possibilities = { make_pair(i, j), make_pair(i, -j), make_pair(-i, j), make_pair(-i, -j), }; if (i != j) { possibilities.push_back(make_pair(j, i)); possibilities.push_back(make_pair(-j, i)); possibilities.push_back(make_pair(j, -i)); possibilities.push_back(make_pair(-j, -i)); } set> visited; deque q; q.push_back(State(0, 0, 0)); int final_cost = -1; while (!q.empty()) { const State& front = q.front(); if (front.x == n-1 && front.y == n-1) { final_cost = front.cost; break; } if (visited.find(make_pair(front.x, front.y)) != visited.end()) { q.pop_front(); continue; } visited.insert(make_pair(front.x, front.y)); for (const auto& p : possibilities) { int x_now = front.x + p.first; int y_now = front.y + p.second; if (inbound(x_now, y_now, n)) { q.push_back(State(x_now, y_now, front.cost + 1)); } } q.pop_front(); } mp.insert(make_pair(input1, final_cost)); if (i != j) mp.insert(make_pair(input2, final_cost)); return final_cost; } int main() { int n; cin >> n; for (int i=1; i < n; ++i) { for (int j=1; j < n; ++j) { if (j != 1) cout << " "; cout << knight(i, j, n); } cout << endl; } return 0; }