#include #include #include #include #include #include #include using node = std::pair; using graph_type = std::map>; graph_type construct_graph(int i, int j, int n) { graph_type graph; std::set visited; std::queue visit; visit.push({ 0,0 }); while(!visit.empty()) { node vnode = visit.front(); visit.pop(); if (vnode.first < 0 || vnode.first >= n || vnode.second < 0 || vnode.second >= n) continue; if (visited.find(vnode) != std::end(visited)) continue; visited.insert(vnode); if (vnode.first + i < n && vnode.second + j < n) { graph[{vnode.first + i, vnode.second + j}].insert(vnode); visit.push({ vnode.first + i, vnode.second + j }); } if (vnode.first - i >= 0 && vnode.second + j < n) { graph[{vnode.first - i, vnode.second + j}].insert(vnode); visit.push({ vnode.first - i, vnode.second + j }); } if (vnode.first + i < n && vnode.second - j >= 0) { graph[{vnode.first + i, vnode.second - j}].insert(vnode); visit.push({ vnode.first + i, vnode.second - j }); } if (vnode.first - i >= 0 && vnode.second - j >= 0) { graph[{vnode.first - i, vnode.second - j}].insert(vnode); visit.push({ vnode.first - i, vnode.second - j }); } if (vnode.second + i < n && vnode.first + j < n) { graph[{vnode.second + i, vnode.first + j}].insert(vnode); visit.push({ vnode.second + i, vnode.first + j }); } if (vnode.second - i >= 0 && vnode.first + j < n) { graph[{vnode.second - i, vnode.first + j}].insert(vnode); visit.push({ vnode.second - i, vnode.first + j }); } if (vnode.second + i < n && vnode.first - j >= 0) { graph[{vnode.second + i, vnode.first - j}].insert(vnode); visit.push({ vnode.second + i, vnode.first - j }); } if (vnode.second - i >= 0 && vnode.first - j >= 0) { graph[{vnode.second - i, vnode.first - j}].insert(vnode); visit.push({ vnode.second - i, vnode.first - j }); } } return graph; } std::map bfs(const graph_type & g) { std::map weights; std::set visited; std::queue q; // initialization weights[{0,0}] = 0; for (auto & n : g) { if (n.first == node{ 0, 0 }) weights[n.first] = 0; else weights[n.first] = std::numeric_limits::max(); } visited.insert({ 0,0 }); auto it = g.find({ 0,0 }); if (it == std::end(g)) return weights; for (auto c : it->second) { weights[c] = 1; q.push(c); } while (!q.empty()) { auto n = q.front(); q.pop(); auto it = g.find(n); for (node c : it->second) { if (visited.find(c) != std::end(visited)) continue; visited.insert(c); weights[c] = std::min(weights[c], weights[n] + 1); q.push(c); } } return weights; } int main() { int n; std::cin >> n; std::vector> ret(n, std::vector(n, 0)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { auto weights = bfs(construct_graph(i + 1, j + 1, n)); ret[i][j] = weights.find({n-1, n-1}) == std::end(weights) ? -1 : weights[{n - 1, n - 1}]; } } for (size_t i = 0; i < ret.size()-1; ++i) { for (size_t j = 0; j < ret.size()-1; ++j) std::cout << ret[i][j] << " "; std::cout << std::endl; } return 0; }