#include #include #include #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; inline void print(int x) { cout << x << " "; } int main(){ int n; cin >> n; // your code goes here for (int i = 1; i < n; i++) // y { for (int j = 1; j < n; j++) // x { int k = max(i, j); int l = min(i, j); { // enough pondering on puttig it in equations :D // let's get on to the brute-force :D bool found = false; int steps = 0; int steps_min = n * n + 1; vector visited(n*n); // bools fill(visited.begin(), visited.end(), 0); auto v = visited.data(); v[0] = 1; // x y list> pos_new, pos_tmp; pos_new.push_back(std::make_pair(0, 0)); for (steps = 1; !pos_new.empty(); steps++) { // pos_new does not contain any move that can reach the detination pos_tmp.clear(); for (auto o = pos_new.begin(), end = pos_new.end(); o != end; o++) { // check where we can go from there int dx, dy; for (int m = 0; m < 8; m++) { // use swicth ?? if (m == 0) { dx = +i; dy = +j; } if (m == 1) { dx = -i; dy = +j; } if (m == 2) { dx = +i; dy = -j; } if (m == 3) { dx = -i; dy = -j; } if (m == 4) { dx = +j; dy = +i; } if (m == 5) { dx = -j; dy = +i; } if (m == 6) { dx = +j; dy = -i; } if (m == 7) { dx = -j; dy = -i; } pair p = *o; p.first += dx; p.second += dy; if (p.first >= 0 && p.first < n && p.second >= 0 && p.second < n && 0 == v[p.first + n * p.second]) { if (p.first == n-1 && p.second == n-1) { steps_min = min(steps, steps_min); found = true; } v[p.first + n * p.second] = 1; pos_tmp.push_back(p); } } // for m } // for i pos_tmp.swap(pos_new); } // for (steps print(found ? steps_min : -1); } } cout << endl; } return 0; }