1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include<bits/stdc++.h> using namespace std; int n, a[100 + 5], bx, by, ex, ey; bool vis[100 + 5][100000 + 5]; int main() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; queue<tuple<int, int, int>>q; cin >> bx >> by >> ex >> ey; q.emplace(bx, by, 0); while (!q.empty()) { tuple<int, int, int>now = q.front(); q.pop(); int x = get<0>(now), y = get<1>(now), d = get<2>(now); if (vis[x][y]) continue; vis[x][y] = 1; if (x == ex && y == ey) { cout << d << endl; return 0; } if (y > 1) q.emplace(x, y - 1, d + 1); if (y <= a[x]) q.emplace(x, y + 1, d + 1); if (x > 1 && a[x - 1] + 1 >= y) q.emplace(x - 1, y, d + 1); if (x < n && a[x + 1] + 1 >= y) q.emplace(x + 1, y, d + 1); if (x > 1 && a[x - 1] + 1 < y) q.emplace(x - 1, a[x - 1] + 1, d + 1); if (x < n && a[x + 1] + 1 < y) q.emplace(x + 1, a[x + 1] + 1, d + 1); } }
|