#include #include #include #include #include #include using namespace std; #define INF 1000000000 int calc_fastest(int pos, int squares[], int goal_idx, vector *footpirnt) { if (pos == goal_idx) { return 0; } if (any_of(footpirnt->begin(), footpirnt->end(), [pos](int x) { return x == pos; })) { return INF; } footpirnt->push_back(pos); int distance = squares[pos]; int steps_when_forward = INF; int steps_when_backword = INF; if (pos + distance <= goal_idx) { printf("move forward to %d, ", pos + distance); steps_when_forward = 1 + calc_fastest(pos + distance, squares, goal_idx, footpirnt); } if (pos - distance >= 0) { printf("move backword to %d, ", pos - distance); steps_when_backword = 1 + calc_fastest(pos - distance, squares, goal_idx, footpirnt); } return min(steps_when_forward, steps_when_backword); } int main() { int n; scanf("%d", &n); int index = 0; int squares[n]; for (int max = 1; 1; max++) { for (int i = 1; i <= max; i++) { if (index + 1 > n) goto BREAK_LABEL; squares[index] = i; index++; } } BREAK_LABEL: vector footprint{}; int fastest = 1 + calc_fastest(0, squares, n - 1, &footprint); if (fastest >= INF) { printf("-1"); } else { printf("%d", fastest); } }