3 条题解
-
1
#include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <cwchar> #include <cwctype> #include <complex.h> #include <deque> #include <exception> #include <fstream> #include <functional> #include <fenv.h> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <inttypes.h> #include <limits> #include <list> #include <map> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <stdbool.h> #include <stdint.h> #include <tgmath.h> #include <utility> #include <vector> using namespace std; #define LL long long const int N = 1e5 + 10; const int INF = 0x3f3f3f3f; struct node{ int pos , step; }; int n , l , r , a[N]; bool v[N]; void bfs(int x){ queue<node> q; q.push((node){x , 0}); v[x] = 1; while(!q.empty()){ node top = q.front(); q.pop(); if(top.pos == r){ cout << top.step; exit(0); } if(top.pos + a[top.pos] <= n && !v[top.pos + a[top.pos]]){ v[top.pos + a[top.pos]] = 1; q.push((node){top.pos + a[top.pos] , top.step + 1}); } if(top.pos - a[top.pos] >= 1 && !v[top.pos - a[top.pos]]){ v[top.pos - a[top.pos]] = 1; q.push((node){top.pos - a[top.pos] , top.step + 1}); } } } int main() { cin >> n >> l >> r; for(int i = 1;i <= n;i++){ cin >> a[i]; } bfs(l); cout << -1; return 0; }
信息
- ID
- 3064
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 129
- 已通过
- 48
- 上传者