计蒜客_跳跃游戏二
BFS
1 #include <iostream> 2 #include <algorithm> 3 #include <queue> 4 using namespace std; 5 typedef pair<int, int> P; 6 int length[110]; 7 int flag[110]; 8 int n; 9 10 int bfs() 11 { 12 priority_queue<P, vector<P>, greater<P> > q; 13 fill(flag, flag + n, 0); 14 q.push(P(0, 0)); 15 while (!q.empty()) { 16 P p = q.top(); 17 q.pop(); 18 if (p.second == n - 1) return p.first; 19 for (int i = length[p.second]; i > 0; --i) { 20 if (!flag[p.second + i]) { 21 flag[p.second + i] = 1; 22 q.push(P(p.first + 1, p.second + i)); 23 } 24 } 25 } 26 return -1; 27 } 28 29 int main() 30 { 31 while (cin >> n) { 32 for (int i = 0; i < n; ++i) cin >> length[i]; 33 cout << bfs() << endl; 34 } 35 return 0; 36 }