#include #include #include #include #include #include using namespace std; struct edge { int to, length; }; const int INT_MAX = 1'000'000'000; int dijkstra(const vector< vector > &graph, int source, int target) { vector min_distance( graph.size(), INT_MAX); min_distance[ source ] = 0; set< pair > active_vertices; active_vertices.insert( {0,source} ); while (!active_vertices.empty()) { int where = active_vertices.begin()->second; if (where == target) return min_distance[where]; active_vertices.erase( active_vertices.begin() ); for (auto ed : graph[where]) if (min_distance[ed.to] > min_distance[where] + ed.length) { active_vertices.erase( { min_distance[ed.to], ed.to } ); min_distance[ed.to] = min_distance[where] + ed.length; active_vertices.insert( { min_distance[ed.to], ed.to } ); } } return -1; } int get_elem(int n, int a, int b) { vector< vector> graph; int dxs[] = {-a, -a, a, a, b, b, -b, -b}; int dys[] = {-b, b, -b, b, a, -a, a, -a}; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { vector edges; for (int k = 0; k < 8; ++k) { auto x2 = i + dxs[k]; auto y2 = j + dys[k]; if ((0 <= x2) && (x2 < n) && (0 <= y2) && (y2 < n)) { //cout << x2 << "/" << y2 << endl; edges.push_back(edge{x2*n + y2, 1}); } } //cout << "edge size: " << edges.size() << endl; graph.push_back(edges); } } //cout << "size: " << graph.size() << endl; return dijkstra(graph, 0, n*n - 1); } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int n; cin >> n; vector> results(n); for (int i=0; i