#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; class cell { public: bool is_reachable; int num_moves; bool has_computed; int x; int y; cell(int x,int y) { this->is_reachable = false; this->num_moves = -1; this->has_computed = false; this->x = x; this->y = y; } }; class ChessBoard { public: vector> cells; int chess_board_size; ChessBoard(int n) { this->chess_board_size = n; for (int i = 0; i < n; i++) { vector temp; for (int j = 0; j < n; j++) { cell *n = new cell(i,j); temp.push_back(n); } cells.push_back(temp); } } bool is_ValidCell(int cur_x, int cur_y) { if (cur_x < 0 || cur_y<0 || cur_x >= chess_board_size || cur_y >= chess_board_size) return false; else return true; } int isReachable(int a, int b,int x,int y) { queue q; map visited; q.push(cells[x][y]); visited.insert({ cells[x][y],true }); int valid_row_moves[] = { -a,-b,a,b,a,-b,-a,b }; int valid_Col_moves[] = { -b,-a,b,a,-b,a,b,-a }; int count = 1; int nextLevel = 0; int depth = 1; bool found = false; while (!q.empty()) { cell *front_cell=q.front(); q.pop(); count--; for (int i = 0; i < 8; i++) { int neighbour_x = front_cell->x + valid_row_moves[i]; int neighbour_y = front_cell->y + valid_Col_moves[i]; if (neighbour_x == 0 && neighbour_y== 0) { found = true; } if (is_ValidCell(neighbour_x,neighbour_y)&&visited.count(cells[neighbour_x][neighbour_y])==0) { q.push(cells[neighbour_x][neighbour_y]); visited.insert({ cells[neighbour_x][neighbour_y],true }); nextLevel++; } } if (found == true) { break; } if (count == 0) { count = nextLevel; nextLevel = 0; depth++; } } return (found == false) ? -1 : depth; } void printMinSteps() { for (int i = 1; i < chess_board_size; i++) { for (int j = 1; j < chess_board_size; j++) { cout << this->isReachable(i, j, chess_board_size - 1, chess_board_size - 1) << " "; } cout << endl; } } }; int main(){ int n; cin >> n; ChessBoard *c = new ChessBoard(n); c->printMinSteps(); return 0; }